I found there has many definition about polymorphism and overloading. Some people said that overloading is one type of polymorphism. While some people said they are not the same. Because only one function will be allocate in overloading. While the polymorphism need allocate the memory for each redefined member function. I really feel confusion about this, any one could explain this for me? Further, whether overloading happens at compile time while the polymorphism happens at running time?
this was told by Bjarne Stroustrup in his book,polymorphism is one which a single thing that act in many forms (i.e) A single function that act in many forms with different parameters. since in c++ constructor name have to be same as class name, you cant have different constructors with different name. so to overcome that function overloading came. many people confuse that function overloading is a type of of polymorphism. they are both at different ends they cant be tied together.
You cannot determine whether a method is a polymorphic method, or simply an overridden method based solely upon its signature. You need to see how the method is invoked.
Here is some sample code which may help illuminate the answer:
public class parentClass {
//Overridden method
public void disp()
{
System.out.println("Output: disp() method of parent class");
}
}
public class childClass extends parentClass {
//You cannot determine whether these methods are polymorphic
//or static polymorphic (aka overridden) simply by their signatures.
//It is by the way they are invoked which determines this.
public void disp(){
System.out.println(" Output: disp() method of Child class");
}
public long add(long a, long b){
return a + b;
}
public int add(int a, int b){
return a+b;
}
public String add(String a, String b){
return a + b;
}
public static void main( String args[]) {
//Here a child class has overridden the disp() method of the
//parent class. When a child class reference refers to the child
//class overriding method this is known as static polymorphism
//or more simply, overriding.
System.out.println("childClass referencing the childClass's overridden, or static polymorphic method");
childClass myChildObj = new childClass();
myChildObj.disp();
//Another example of static polymorphic, or overridden methods
System.out.println("The following are overridden, or static polymorphic methods:");
System.out.printf(" Long add()override method results: %d \n",
myChildObj.add(5999999, 1));
System.out.printf(" Integer add() override method results: %d \n", myChildObj.add(3,2));
System.out.printf(" String add() override method results: %s \n",
myChildObj.add(" First and ...", " Second"));
//When the parent class reference refers to the child class object
//then the overriding method is called.
//This is called dynamic method dispatch and runtime polymorphism
System.out.println("True polymorphism, when the parent class object calls the child class's method:");
parentClass myParentObj = new childClass();
myParentObj.disp();
}
}
The expected output:
childClass referencing the childClass's overridden, or static polymorphic method
Output: disp() method of Child class
The following are overridden, or static polymorphic methods:
Long add()override method results: 6000000
Integer add() override method results: 5
String add() override method results: First and ... Second
One example of true polymorphism, when the parent class object calls the child class's method:
Output: disp() method of Child class
Polymorphism is the process to define more than one body for functions/methods with same name.
Overloading IS a type of polymorphism, where the signature part must be different. Overriding is another, that is used in case of inheritance where signature part is also same.
No, it's not true that polymorphism happens in runtime. What happens in runtime is called runtime polymorphism. That is implemented using virtual
keyword in C++.
Hope it helped..
The polymorphism is the base of the OOP, the overloading is one of ways to implement to polymorphism, specially when are involved operators. More generally, speaking about polymorphism when there are two or more classes involved. While the overloading can be made also inside the same class, we can overload the name of a method with several signatures (different list of parameters). While overriding is designed exclusively for involving two or more classes. Note that the overrided methods have all the same signature.
The word itself exlains the clear meaning. 'Poly' means multiple, while 'morphism' (used in image technology) means the process of gradual change from one form to another. Thus, same thing will have different forms. Technically, Polymorphism is way of implementing 'Single Interface (Skeleton or structure) multiple Implementation (content or body)'. Polymorphism is a general term which refers to both overloading and overriding. Overloading is done in same class where the functions or methods with the same name have different signatures (argument list or return type) while overriding comes in picture in case of inheritance where a function interface, in the Super class, has similar interface in the subclass and has different implementation than the one in super class. The Super class and sub class form a hierarchy moving from lesser specialization to greater specialization and this should always be remembered while implementing overriding of functions.
Polymorphism is based on following 2 concepts:
- Overloading(Compile-Time polymorphism): Methods with same name but different operation
- Overriding(Run-Time polymorphism): Override a method in base class by creating similar method in derived class
-Corrected the Explainations Shyam Kodase
Overloading is just a way of providing multiple ways of calling the same method/function/constructor with default values, less arguments etc.
Polymorphism is about object inheritance, sub classes etc.
来源:https://stackoverflow.com/questions/22887349/difference-between-polymorphism-and-overloading