What is the difference between function polymorphism and overloading?
Â
I'm still not clear about the distinction between 'polymorphism' and 'overloading'.
Could you please let me know how to differentiate between the two?
Thanks.
Â
I'm still not clear about the distinction between 'polymorphism' and 'overloading'.
Could you please let me know how to differentiate between the two?
Thanks.
Short answer:Â
They are the same.Â
Long Answer, (and yet less revealing):Â
Polymorphism is simply the ability to have many different methods (Or functions, for those who are used to C-Type programs) to have the same name, but act differently depending on the type of parameters that were passed to the function.Â
So for example, we may have a method called punch, which accepts no parameters at all, and returns an integer:Â
public int punch()Â
{Â
return 3;Â
}Â
We could also have a method named punch that accepts a String and returns a boolean.Â
public boolean punch(String poorGuyGettingPunched)Â
{Â
if(poorGuyGettingPunched.equals("joe"))Â
{Â
System.out.println("sorry Joe");Â
return true;Â
}Â
elseÂ
return false;Â
}Â
That is called polymorphism… And strangely enough, it is also called overloading.Â
Do not confuse this with overriding, which replaces a function or method with a new one, or rather, hides the old method and replaces it with a new one.
Â
Hi Josella,
They are more like the same, but the difference comes in when the actual method to execute is to be determined:
For instance:
record=newRecord()
record=newRecord(1000)
The compiler will tell which constructor to use just by the method signature, the number and types of parameters too. The method to be used for execution is determined by early binding.
Access the following link to read more: