Friday, October 5, 2012

Overloading and Overriding

What is method overloading ?
Writing two or more methods in the same class in such a way that each method has same name but with different method signatures – is called method overloading.
What is method overriding ?
Writing two or more methods in super and sub classes such that the methods have same name and same signature is called method overriding.


Method Overloading
Writing two or more methods with the same name but with different signatures is called method overloading.
Method overloading is done in the same class.
In method overloading, method return type can be same or different.
JVM decides which method is called depending on the difference in the method signatures.
Method overloading is done when the programmer wants to extend the already available features.
Method overloading is code refinement. Same method is refined to perform a different task.

Method Overriding
Writing two or more methods with the same name and same signatures is called method overriding.
Method overriding is done in super and sub classes.
In method overriding method return type should also be same.
JVM decides which method is called depending on the data type (class) of the object used to call the method.
Method overriding is done when the programmer wants to provide a different implementation(body) for the same feature.
Method overriding is code replacement. The sub class method overrides(replaces) the super class method.

No comments:

Post a Comment