Method Overloading Method Overriding
Method overriding is when one of the methods
Method overloading is in the same class, where
in the super class is redefined in the sub-class.
more than one method have the same name but
In this case, the signature of the method
different signatures.
remains the same.
Ex:
class X{
public int sum(){
// some code
Ex:
}
}
void sum (int a , int b);
void sum (int a , int b, int c);
class Y extends X{
void sum (float a, double b);
public int sum(){
//overridden method
//signature is same
}
}
Difference between Overloading and Overriding
Difference between Static & Dynamic Polymorphism
Static Polymorphism Dynamic Polymorphism
It relates to method overloading. It relates to method overriding.
In case a reference variable is calling an
overridden method, the method to be
Errors, if any, are resolved at compile
invoked is determined by the object,
time. Since the code is not executed
your reference variable is pointing to.
during compilation, hence the name
This is can be only determined at
static.
runtime when code in under execution,
Ex: hence the name dynamic.
void sum (int a , int b);
Ex:
void sum (float a, double b);
//reference of parent pointing to child
int sum (int a, int b); //compiler
object
gives error.
Doctor obj = new Surgeon();
// method of child called
obj.treatPatient();