In this post we will discuss about method overloading and method overriding in java with examples. This is one of important interview questions for 0 to 3 years of experienced Java Developers.Let us know first what is overloading and overriding in java and then we will learn Differences between them.
Method Overloading:
If a class have multiple methods by same name but different parameters is known as Method overloading. The main advantage of overloading increase the readability of the program.
Example:
The following example shows by changing no of arguments
Method Overloading:
If a class have multiple methods by same name but different parameters is known as Method overloading. The main advantage of overloading increase the readability of the program.
Example:
The following example shows by changing no of arguments
class Calculation
{
void sum(int a,int b){
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);}
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
Output:
30
40
Method Overriding:
What ever the parent has by default available to child class through inheritance,if the child class is not satisfied with the parent class implementation then the child class is allowed to override the parent class method to provide its own specific implementation this is nothing but method overriding.
Example:
class Bank{
int getRateOfInterest(){
return 0;}
}
class SBI extends Bank{
int getRateOfInterest()
{
return 7;}
}
class ICICI extends Bank{
int getRateOfInterest(){
return 8;}
}
class AXIS extends Bank{
int getRateOfInterest(){
return 9;}
}
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
Output:
SBI Rate of Interest: 7
ICICI Rate of Interest: 8
AXIS Rate of Interest:9
Now we will see differences between them
Method Overlaoding:
Method Overriding:
{
void sum(int a,int b){
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);}
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
Output:
30
40
Method Overriding:
What ever the parent has by default available to child class through inheritance,if the child class is not satisfied with the parent class implementation then the child class is allowed to override the parent class method to provide its own specific implementation this is nothing but method overriding.
Example:
class Bank{
int getRateOfInterest(){
return 0;}
}
class SBI extends Bank{
int getRateOfInterest()
{
return 7;}
}
class ICICI extends Bank{
int getRateOfInterest(){
return 8;}
}
class AXIS extends Bank{
int getRateOfInterest(){
return 9;}
}
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
Output:
SBI Rate of Interest: 7
ICICI Rate of Interest: 8
AXIS Rate of Interest:9
Now we will see differences between them
Method Overlaoding:
- Method name should be same and different parameters that means method signature should be different
- No restriction for any return type ,it might be same or different return type in overloading
- Private,static and final methods are applicable for overloading.
- No restrictions for Access Modifiers for overloading
- No restrictions for throws class regarding exception handling in Overloading
- In overloading one method can not hide the other method
- Overloading is also called as compile time polymorphism or static polymorphism or early binding.
Method Overriding:
- Method name should be same and parameters also must be same that means method signature must be same
- In overriding return type must be same upto JDK 1.4 version,later version there is a feature co-varient return type.So need not be same return type
- Final method can not overridden
- If child class throws any checked Exception(IOException) then the parent class should throw either same checked Exception or its parent exception
- In overriding sub class method hides that of the super class method.
- Overriding is also know as Runtime polymorphism or Dynamic polymorphism or late binding.
No comments:
Post a Comment