Difference between overloading and overriding

In the last post, we have read the overloading in java and overriding in java. We have seen many examples with different scenarios. In this post, we will see what is the difference between method overloading and method overriding or the difference between overloading and overriding. It is the most common question in interviews and most of the programmer gets confused to find the difference between method overloading and method overriding.

Method Overloading

1.  Method overloading in java possible within the class. Method overloading in Java means multiple methods having the same name but different in parameters. A class can hold several methods having the same name, but different types/order/number of parameters.

difference between method overloading and method overriding

2. Method Overloading happens at compile-time. In method overloading the compiler binds the call at compile time.

3. In method overloading, it is possible to overload the Static methods.

4. In method overloading, it is possible to overload the final methods.



5. In method overloading, it is possible to overload the private methods.

6.  Method overloading is possible within class. Because we can define any number of methods with same name but different parameters.

7. You can’t overload the method based on the return type. The return type of overloaded method does not matter. It can be the same or different.

Method Overriding

1. Method overriding in java is only possible with the concept of Inheritance. As you know by use of inheritance concept in Java, we can inherit the method of the parent class to child classMethod overriding in Java is a feature through which we can define the method in child class that is already defined in parent classes. If a method in a child class has the same name, and parameters as a method defined in parent class, then the method is defined in the subclass is called overriding method and the method defined in the parent class is called an overridden method.

difference between method overloading and method overriding

2. Method overriding happens at run-time. In method overriding the compiler binds the call at run time.


3. In method overriding we can’t override the static methods. If we override the static method, it is considered as method hiding.

4. In method overriding we can’t override the final methods.

5. In method overriding we can’t override the private methods. Because the private method is not inherited in child class.

 6. Method override is not possible within the class. Method overriding is only possible by the use of inheritance.  

7. After Java 5, Java allows us to change the return type of the overriding method(Method in child class). But the return type of overriding method (Method of child class) must be a subtype of the overridden method (Method of Parent class). 

1 thought on “Difference between overloading and overriding”

  1. This is definitely a website that people need to get behind. The problem is, no one wants to do a huge deal of reading and not have something else to stimulate the mind. This is the internet, after all. Maybe if you added a video or two to emphasise your point. Ill stick around, FOR SURE. But, I dont know if others will.

    Reply

Leave a Comment