Polymorphism in Java : Part 2
After having understood the basics of Polymorphism and what it actually refers to in my previous post, now lets get on and try understanding Method Overloading in a little more detailed fashion.
In this post, as promised, I shall be talking about the following
- How is method overloading achieved?
- What are the criteria’s for a valid overloading?
- Why is method overloading called Compile time polymorphism?
Method overloading is primarily achieved as already discussed by changing the number or types of the arguments in the method. Changing the name of the arguments alone is not significant and therefore doesn’t lead to a condition wherein an overload is called a valid overload.
The following are the conditions for a method to be called a valid overloaded method in Java.
- A valid overloaded method should change the number or types of the arguments of the method.
- A valid overloaded method may change the return types of the method.
- A valid overloaded method can vary the access levels in any direction in a method.
Following is a valid examples of method overloading in Java
//Class Person which implements method overloading.
class Person {
private String firstName;
private String lastName;
//Constructor
Person() {
this.firstName = “”;
this.lastName = “”;
}
// Overloaded constructor taking one string as argument.
Person(String lname) {
this.firstName = “”;
this.lastName = lname;
}
//Overloaded constructor taking two string parameters as arguments.
Person(String fname, String lname) {
this.firstName = fname;
this.lastName = lname;
}
}
Before signing off this post, i will also try explaining the reason for calling Method Overloading as Compile Time Polymorphism. Well, the reason is pretty straight, whenever you write an overloaded method and try to call it using an object of the class, you specify the arguments correctly. The compiler therefore is able to decide while compiling itself about the method to be called and therefore the name Compile Time Polymorphism.
Hope you enjoyed reading this. In the next post, i shall be talking about Method Overriding and Run Time Polymorphism.
Related Reads: Polymorphism in Java : Part 1
You can follow me on Twitter at http://twitter.com/vaibhav1981
Do stay tuned to Technofriends for more, one of the best ways of doing so is by subscribing to our feeds. You can subscribe to Technofriends feed by clicking here.
Related posts:
- Polymorphism in Java : Part 1
- Java: Lets revisit the past
- How-To: Read an XML file using DOM parser in JAVA?
- [How-To] Print Date/Time in a Given Format in Java
- [Java] Reading a Webpage through URL

[...] Polymorphism in Java : Part 2 [...]
Hi i find it quite interesting, because the examples given are easy to understand.
thanks a lot.
It was very useful
Hi,
This is really easy to understand the code as well as concept.
Hi
It was really useful.Thanks a lot…..
Nice post…thanks a lot….examples are really very easy to understand the concept
Lots of of bloggers not really happy with the new iPad.There was too much hoopla over it and lots of people got turned off.You see, I can actually see some of the awesome potential uses of the gizmo. Third-party apps for working with tunes, games, newspapers and magazines and books, all kinds of good stuff, but IMHO they failed to sell it very well (excluding the books). It smells rather incomplete