Polymorphism, Discussion on Overloading And Overriding with Examples
Polymorphism: Having many shapes, occurrence of different forms.
Overloading: Ability of a request (message / method call) to produce different results based on the request parameters.
Overloading: Ability of a request (message / method call) to produce different results based on the request parameters.
- Overloaded methods must have same name.
- Overloaded methods must differ in input parameters’ type or order or number
- Overloaded methods must NOT differ ONLY in their return types(i.e., having same name & input parameters).
Do overloaded methods have same method signatures ?
No overloaded methods have not same method signatures.
Following are some examples of overloaded methods.
Example1:
package
com.aurangzeb.utk;
public class
OverloadedMethods {
public void printData(int age, String
name) {
}
public void
printData(String name, int age) {
}
// Yes order
of input parameters is different
}
package
com.aurangzeb.utk;
public class
OverloadedMethods {
int addValue(int a, int b) {
return 1;
}
int addValue(int a, int b, int c) {
return 2;
}
// Yes numer
of Input Parameters is different
}
Example3:
package
com.aurangzeb.utk;
public class
OverloadedMethods {
int addValue(int a, int b) {
return b;
}
float addValue(float a, float b) {
return b;
}
// Yes, Type of input parameters is different
}
Example4:
package
com.aurangzeb.utk;
public class
OverloadedMethods {
void printName(String
fName, String lName){
}
void printName(String
lName, String fName){
}
//No, same
type, order and number of input parameters
}
Overriding:Ability of a request (message / method call) to produce different results based on the object it is sent to.
- A language feature in OOP, that allows a subclass to provide a specific implementation of a method that is already provided by one of its super-classes
- The implementation in the subclass overrides (replaces) the implementation in the super-class.(
- Same method signature
- Same Name
- Same Input Parameters
- Same Return Type
- But Multiple implementations !!!

thanks for explaining in very simple way. Great effort.... Cheers!!!!!
ReplyDeleteif you have any query about the overriding and overlaoding please post ur comments. thanks :)
Delete