First let us see what is method overloading and why we do so :
The purpose of method overloading is re-usability of code. The same method name can be used again and again if their functionality is relevant, say you are calculating the area of different shapes, so you can make a method and use it again and again with different parameters for different shapes. Let us have a simple example :
public class Demo{
public void CalSum(int a,int b){
int sum=a+b;
System.out.println(sum);
}
public void CalcSum(int a,int b,int c){
int sum=a+b+c;
System.out.println(sum);
}
public static void main(String[] args){
Demo d1=new Demo();
d1.CalSum(10,20);
d1.CalSum(10,20,30);
}
}
Output= 30 60
Here, we are using the same method name again in same class, but for different sum calculation.
So we can define method overloading as "Same method name, but different behavior".
Rule for method overloading:
1. Argument list must be different
2. Return type of method do not matter, so you can change it
3. Access specifier also does not matter,that too you can change
4. method overloading is done in same class
Here, call to overloaded method is resolved at compile time. So only necessary condition for method overloading is that compiler should be able to distinguish between overloaded method.
That's why the argument list must be different.
Let us see some more valid overloaded versions of CalSum :
public void CalSum(int a,long b){ }
public void CalSum(long a,int b){ }
public int CalSum(long a,long b){ } // here return type is different, but it does not matter
So keep in mind that only compiler should be able to differentiate between original method and overloaded method.
method overloading is also known as compile time polymorphism.
Imp : When it comes to Exception, overloaded methods should not throw broader checked exception.
method overriding:
method overriding comes into picture in inheritance, when there is super class -sub class relationship.
why we do method overriding :
Suppose you are defining the characteristics of each animal, so you will require methods like eat(),sleep() etc. Every animal eats and sleeps, So instead of defining these methods again and again, have a super class as Animal and declare eat() and sleep() in it. And just override these methods by extending Animal class.
In real life, there can be more number of methods, so at that time method overriding plays a vital role
Example :
class Animal{
public void eat(){
System.out.println("Animal eats"); // default implementation to eat()
}
public void sleep(){
System.out.println("Animal sleeps"); // default implementation to sleep()
}
}
class Horse extends Animal{
public void eat(){
System.out.println("Horse eats");
}
public void sleep(){
System.out.println("Horse sleeps");
}
public void run(){
System.out.println("Horse runs"); // Horse specific run method.
}
}
public class Test{
public static void main(String[] args){
Animal a1=new Horse();
a1.eat(); // invokes overridden eat()
a1.sleep(); // invokes overridden sleep()
a1.run(); // invokes Horse specific run()
}
}
output : Horse eats Horse sleeps Horse runs
Here, call to overridden method is decided at run time and type of object decides which method to call.
method overriding is also known as runtime/dynamic polymorphism, where call to overridden method is resolved at run time by the type of object
Rules :
1. Argument list must be same
2. you can change return type of overridden method, but it must be co-variant return type
3. you can change access specifier of overridden method, but it should be less restrictive like if it is package level then you can make it as public but reverse not allowed.
4. overridden method must not throw broader checked exception
That's it for today, there are more things regarding this topic, will see that soon..
The purpose of method overloading is re-usability of code. The same method name can be used again and again if their functionality is relevant, say you are calculating the area of different shapes, so you can make a method and use it again and again with different parameters for different shapes. Let us have a simple example :
public class Demo{
public void CalSum(int a,int b){
int sum=a+b;
System.out.println(sum);
}
public void CalcSum(int a,int b,int c){
int sum=a+b+c;
System.out.println(sum);
}
public static void main(String[] args){
Demo d1=new Demo();
d1.CalSum(10,20);
d1.CalSum(10,20,30);
}
}
Output= 30 60
Here, we are using the same method name again in same class, but for different sum calculation.
So we can define method overloading as "Same method name, but different behavior".
Rule for method overloading:
1. Argument list must be different
2. Return type of method do not matter, so you can change it
3. Access specifier also does not matter,that too you can change
4. method overloading is done in same class
Here, call to overloaded method is resolved at compile time. So only necessary condition for method overloading is that compiler should be able to distinguish between overloaded method.
That's why the argument list must be different.
Let us see some more valid overloaded versions of CalSum :
public void CalSum(int a,long b){ }
public void CalSum(long a,int b){ }
public int CalSum(long a,long b){ } // here return type is different, but it does not matter
So keep in mind that only compiler should be able to differentiate between original method and overloaded method.
method overloading is also known as compile time polymorphism.
Imp : When it comes to Exception, overloaded methods should not throw broader checked exception.
method overriding:
method overriding comes into picture in inheritance, when there is super class -sub class relationship.
why we do method overriding :
Suppose you are defining the characteristics of each animal, so you will require methods like eat(),sleep() etc. Every animal eats and sleeps, So instead of defining these methods again and again, have a super class as Animal and declare eat() and sleep() in it. And just override these methods by extending Animal class.
In real life, there can be more number of methods, so at that time method overriding plays a vital role
Example :
class Animal{
public void eat(){
System.out.println("Animal eats"); // default implementation to eat()
}
public void sleep(){
System.out.println("Animal sleeps"); // default implementation to sleep()
}
}
class Horse extends Animal{
public void eat(){
System.out.println("Horse eats");
}
public void sleep(){
System.out.println("Horse sleeps");
}
public void run(){
System.out.println("Horse runs"); // Horse specific run method.
}
}
public class Test{
public static void main(String[] args){
Animal a1=new Horse();
a1.eat(); // invokes overridden eat()
a1.sleep(); // invokes overridden sleep()
a1.run(); // invokes Horse specific run()
}
}
output : Horse eats Horse sleeps Horse runs
Here, call to overridden method is decided at run time and type of object decides which method to call.
method overriding is also known as runtime/dynamic polymorphism, where call to overridden method is resolved at run time by the type of object
Rules :
1. Argument list must be same
2. you can change return type of overridden method, but it must be co-variant return type
3. you can change access specifier of overridden method, but it should be less restrictive like if it is package level then you can make it as public but reverse not allowed.
4. overridden method must not throw broader checked exception
That's it for today, there are more things regarding this topic, will see that soon..