Overloading & Overriding in Java
Overloading
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.
Overriding
If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
* Main difference comes form the fact that method overloading is resolved during compile time, while method overriding is resolved at runtime
class Fruits{
void display(Fruits frts) {System.out.print("Fruits");}
}
class Mango extends Fruits{
void display(Mango mngo) {System.out.print("Mango");}
}
class Alphonso extends Mango{
void display(Alphonso alf) {System.out.print("Alphonso");}
}
public class test {
public static void main(String[] args) {
Fruits c1 = new Alphonso(); Alphonso c2 = new Alphonso();c1.display(c2);
}}
Output of this code is
But if I modify class Fruits as:
The type of
So in the first case, the compiler is going to call
Now in the first case, the
In the second case,
So remember:
Overloading
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.
Overriding
If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
* Main difference comes form the fact that method overloading is resolved during compile time, while method overriding is resolved at runtime
class Fruits{
void display(Fruits frts) {System.out.print("Fruits");}
}
class Mango extends Fruits{
void display(Mango mngo) {System.out.print("Mango");}
}
class Alphonso extends Mango{
void display(Alphonso alf) {System.out.print("Alphonso");}
}
public class test {
public static void main(String[] args) {
Fruits c1 = new Alphonso(); Alphonso c2 = new Alphonso();c1.display(c2);
}}
Output of this code is
'Fruits'.But if I modify class Fruits as:
classFruits{ void display(Alphonso frts) {System.out.print("Fruits");} }
It will display "Alphonso"
The type of
c1 is Fruits. Therefore when working out which method signature it's going to call, the compiler can only look at methods declared in Fruits.So in the first case, the compiler is going to call
display(Fruits); in the second case, the compiler is going to call display().Now in the first case, the
display(Fruits) method is never overridden, so actually the execution-time type of c1 is irrelevant.In the second case,
display(Alphonso) is overridden by alf, so the implementation in alf is called because the execution-time type of c1 is alf.So remember:
- Overload resolution (which method signature is called) is determined at compile-time, based on the compile-time types of both the method target and the argument expressions
- The implementation of that method signature (overriding) is based on the actual type of the target object at execution time.
Comments
Post a Comment