Difference between Method Overloading and Method Overriding in Java


Method Overloading in Java

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.

Example of Method Overloading

If you observe the following example, here we have created a class called Sample and this class has two methods with the same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables).

When you invoke the add() method based on the parameters you pass respective method body gets executed.

public class Sample {
   public static void add(int a, int b) {
      System.out.println(a+b);
   }
   public static void add(int a, int b, int c) {
      System.out.println(a+b+c);
   }
   public static void main(String args[]) {
      Sample obj = new Sample();
      obj.add(20, 40);
      obj.add(40, 50, 60);
   }
}

Output

On execution, it will produce the following output −

60
150

Method Overriding in Java

In method overriding, Super class and subclass have methods with the same name including parameters. JVM calls the respective method based on the object used to call the method. In overriding, the return types should also be same.

Example of Method Overriding

Let's take an example to understand how method overriding works in Java −

class SuperClass {
    public static void sample() {
        System.out.println("Method of the super class");
    }
}
public class SubClass extends SuperClass {
    public static void sample() {
        System.out.println("Method of the sub class");
    }
    public static void main(String args[]) {
        SuperClass obj1 = new SubClass();
        SubClass obj2 = new SubClass();
        obj1.sample();
        obj2.sample();
    }
}

Here we have a SuperClass and a SubClass. Both the classes have a method called Sample() with the same signature. In the main class, we created obj1 for SuperClass and obj2 for SubClass. The JVM calls the respective method based on the object used to call the method.

Output

On execution, it will produce the following output −

Method of the super class
Method of the sub class

Difference between Overloading and Overriding

The following table highlights the major differences between Method Overloading and Method Overriding −

Method Overloading

Method Overriding

Method overloading is known as compile-time polymorphism.

Method overriding is known as runtime polymorphism.

For overloading to come into picture, there must be at least two methods of the same name.

For overriding to work, we need to have at least one method with the same name in both the parent class as well as the child class.

The methods must have different number of parameters. If both the methods have the same number of parameters, then their type must be different.

Both the methods must have the same number of parameters with the same type.

Conclusion

Overloading and Overriding are concepts in object-oriented programming that are used to improve the readability and reusability of the programs. Method overloading is a type of static polymorphism. In Method overloading, we can define multiple methods with the same name but with different parameters.

Method Overriding is a mechanism to achieve polymorphism where the super class and the sub-class have same methods, including the parameters and signature. The JVM calls the respective method based on the object used to call the method.

Updated on: 13-Sep-2023

31K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements