This article demonstrates the difference between method overloading and method overriding in Java with examples
Method overloading and method overriding are both OOP (object-oriented programming) concepts highly used in variety of Java implementations. We already wrote about the 4 major concepts of OOP in this article. If you are unfamiliar with OOP please check this article first.
Method overloading vs. Method overriding is a common Java job interview question. Going to an interview, we can not afford to not know the difference.
What is method overloading?
As you will see in the example below, method overloading gives us the ability to have multiple methods with the same name and same or different return type. The important thing about method overloading is that all this methods must have different parameters for the same return type. It is a common practice to leave the implementation for the method with the most parameters and the other methods (with fewer parameters) just redirect to the “big” method, giving default values to missing parameters like this:
public int calculate(int data[]) { return calculate("Default calculation", data, 0); } public int calculate(String name, int data[]) { return calculate(name, data, 0); } public int calculate(String name, int data[], int offset) { // do all the calculations here }
What is method overriding?
Method overriding is part of the Java inheritance OOP principle. The idea behind method overriding is to change the implementation of given method in a subclass. In other words you “override” the implementation of the parent’s class method using the same signature of the method (name, return types, parameters), but implement different functionality inside the overridden method.
Method overloading example in Java
Following example demonstrates usage of method overloading. We have 4 methods with same name and different parameters and return types
package net.javatutorial; public class OverloadingExample { static int sumOf(int a, int b) { return a+b; } static int sumOf(int a, int b, int c) { return a+b+c; } static double sumOf(double a, double b) { return a+b; } static double sumOf(double a, double b, double c) { return a+b+c; } public static void main(String[] args) { System.out.println(sumOf(1,2)); System.out.println(sumOf(10d,20d,30d)); } }
The output of the program is:
3 60.0
As you see in the example above, a different method is called based on the parameters we use. This is a nice way to increase the readability of the program.
Method overriding example in Java
Animal superclass
package net.javatutorial; public class Animal { public void makeSound() { System.out.println("the animal makes sounds"); } }
Dog subclass has the same method but with different behaviour
package net.javatutorial; public class Dog extends Animal{ @Override public void makeSound() { System.out.println("the dog barks"); } }
Note the usage of @Override annotation. It is not mandatory, but it’s a good practice to annotate overridden methods for better code readability.
Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading. Thanks for sharing the difference.
Hey,it was a great article.I found it very useful for me please keep posting such articles.I would love to learn form your article. Thanks.
Please correct the definition of Method overloading. we can have diff return type too.
I did this : “multiple methods with the same name and same or different return type” 🙂