Difference Between Method Overriding VS Method Overloading in Java

Method Overriding VS Method Overloading in Java: In this article, we will list the difference between method overriding and method overloading in Java. Before moving ahead with the differences, read the detailed concepts about method overloading and method overriding in the following articles

  • Method Overriding in Java
  • Method Overloading in Java

Let us detail out the difference between method overriding v/s method overloading in tabular form below

Method Overloading vs Method Overriding

Method Overloading Method Overriding
If any class contains multiple methods with an exact same name but with different input parameter list then it is known as method overloadingIf a subclass has the same instance method with the same method signature as that of the super class’s method then it is said to be method overriding
Method name should be the same but with different number of input parameters or data-type of input parameters (includes order/sequence of input parameters)Method signature should be same both in superclass as well as in subclass (including access modifier, return type & exception of method signature)
Method signature has to be differentMethod signature should be the same
Input parameter lists have to be differentInput parameter lists should be the same even their data-types and order/sequence should the same
Overloading happens in the same class (just one class)Overriding happens in 2 or more classes through inheritance concept
This provides multiple implementation version with same method name in the same classThis provides a specific implementation in subclass when extending from super class’s more general implementation
This is resolved at compile-time, therefore, it is also known as compile-time polymorphismThis is resolved at run-time, therefore, it is also known as run-time polymorphism
Sometimes, this is referred to as static binding as method call resolves during compilationAnd this is referred to as dynamic binding as method calls resolve during execution
Method overloading increases the readability of the programThis is used to provides a specific implementation in the extending class
The return type can be the same or different in case of method overloading as it doesn’t get countedReturn type has to be same from that of super class’s return type (or else it should sub-class or sub-type of super class’s return type)
This is called co-variant return type
Overloading gives better performance as it is resolved during a compile-timeOverriding performance is slightly on the lower side as compared to overloading
non-access modifiers like static or final aren’t get accounted in method overloadingTherefore, overloaded methods can have the static, final keyword in a method signatureFinal methods cannot be overridden (this is inheritance concept)Static methods cannot be overridden, rather it can be re-declared in the subclass
Also, access modifiers like private aren’t get accounted in method overloadingPrivate methods cannot be overridden (again, this is inheritance concept)
Read method overloading rules Read method overriding rules

Example of Method Overloading

package in.bench.resources.java.overload;
public class TestJavaOverload {
    void add(int num1, float num2) {
        System.out.println("The summation of 2 numbers : " + (num1 + num2));
    }
    void add(int num1, float num2, int num3) {
        System.out.println("The summation of 3 numbers : " + (num1 + num2 + num3));
    }
    public static void main(String args[]) {
 
        TestJavaOverload t1 = new TestJavaOverload();
        t1.add(12, 16f); // invoking 1st method with 2 arguments
        t1.add(10, 20f, 30); // invoking 1st method with 3 arguments
    }
}

Output:

The summation of 2 numbers : 28.0 
The summation of 3 numbers : 60.0

Example of Method Overriding

Superclass

package in.bench.resources.method.overriding;
public class Shape {
    void draw() throws Exception {
        System.out.println("Super class >> Shape : draw() method");
    }
}

Subclass

package in.bench.resources.method.overriding;
public class Circle extends Shape {
    @Override
    protected void draw() throws Exception {
        System.out.println("Sub class >> Circle : draw() method");
    }
}

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment