Hey guys in this article, you will understand the difference between Method overloading and method overriding in Java with easy to understand example
Read More:
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
- Check the Spring Boot JdbcTemplate Tutorials
Table of Contents
Method Overloading vs Method Overriding
Following are the difference between Method overloading and Method overriding
Method Overloading | Method Overriding |
Method overloading is used to increase the readability of the program. | Method overriding is used to provide the specific implementation of the method that is already provided by its super class. |
Method overloading is performed within class. | Method overriding occurs in two classes that have IS-A (inheritance) relationship. |
In case of method overloading, parameter must be different. | In case of method overriding, parameter must be same. |
Method overloading is the example of compile time polymorphism. | Method overriding is the example of run time polymorphism. |
In java, method overloading can’t be done by changing only the return type of method. Return type can be same/different in overloading, but you must change the parameter. | Return type must be same or covariant (changing return type to subclass type) in method overriding. |
Method Overloading
Advantage of method overloading
Method overloading increases the readability of the program.
Different ways to overload the method
There are two ways to overload the method in java
- By changing number of arguments
- By changing the data type
In java, Method Overloading is not possible by changing the return type of the method only.
Why Method Overloading is not possible by changing the return type of method only?
In java, method overloading is not possible by changing the return type of the method only because of ambiguity.
Example
Following is an example that demonstrates the Method overloading
public class Calculate {
public static void main(String[] args) {
Calculate c = new Calculate();
System.out.println(c.add(2, 4));
System.out.println(c.add(2.5, 4.5));
}
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
Method Overriding
Usage of Java Method Overriding
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
- Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
- The method must have the same name as in the parent class
- The method must have the same parameter as in the parent class.
- There must be an IS-A relationship (inheritance).
Can we override static method?
No, a static method cannot be overridden. It can be proved by runtime polymorphism.
Why can we not override static method?
It is because the static method is bound with class whereas instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area.
Can we override java main method?
No, because the main is a static method.
Example
Following is an example that demonstrates the Method overriding
class Parent{
void display(){
System.out.println("Parent");
}
void print(){
System.out.println("Parent Print Method");
}
}
class Child extends Parent{
@Override
void display(){
System.out.println("Child");
}
}
public class OverridingDemo{
public static void main(String[] args) {
Child objChild = new Child();
objChild.display();
//Parent method will be called
objChild.print();
}
}
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.