Signup/Sign In
LAST UPDATED: JANUARY 9, 2023

Difference Between Method Overloading and Method Overriding

Because of its different ideas and characteristics such as classes, objects, inheritance, and so on, Java is often regarded as one of the greatest languages for object-oriented programming. Polymorphism is one of the most fundamental and crucial ideas in the object-oriented programming paradigm, which Java supports.

Polymorphism is a term that simply implies "having multiple forms." It refers to the idea that various sorts of things may be accessible using the same interface in computer science. Each of the many kinds may provide its own implementation of the interface.

Difference Between Method Overloading and Method Overriding

What is Method Overloading?

In Java, Method overloading refers to the notion of having many methods in the same class with the same name but distinct argument lists. We may construct methods that do comparable activities under the same name by using method overloading.

Compile-time polymorphism, often known as static or early binding, is an example of method overloading. When we use compile-time polymorphism, we only tie an object to its functionality at build time. In Java, the JVM knows the method or object it will execute for a given call before it executes the code.

Example of Method Overloading in Java:

In this example, we have two sum() methods that take integer and float type arguments respectively.

Notice that in the same class we have two methods with same name but different types of parameters

class Calculate
{
  void sum (int a, int b)
  {
    System.out.println("sum is"+(a+b)) ;
  }
  void sum (float a, float b)
  {
    System.out.println("sum is"+(a+b));
  }
  Public static void main (String[] args)
  {
    Calculate  cal = new Calculate();
    cal.sum (8,5);      //sum(int a, int b) is method is called.
    cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
  }
}

Sum is 13 Sum is 8.4

You can see that sum() method is overloaded two times. The first takes two integer arguments, the second takes two float arguments.

What is Method Overriding?

Method overriding is the process of redefining a superclass function in a subclass with the same method signature. The methods must have the same name, the same number of arguments, and the same kind of parameters in the same order if they have the same signature. It's used to modify the behavior of existing methods and to give fine-grained implementations of methods specified in a superclass in subclasses.

Example of Method Overriding in Java:

Below we have simple code example with one parent class and one child class wherein the child class will override the method provided by the parent class.

class Animal
{
    public void eat()
    {
        System.out.println("Eat all eatables");
    }
}

class Dog extends Animal
{
    public void eat()   //eat() method overridden by Dog class.
    {
        System.out.println("Dog like to eat meat");
    }
    
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();
    }
}

Dog like to eat meat

Comparison Table Between Method Overloading And Method Overriding

Method Overloading Method Overriding
  • When two or more methods have the same name but distinct parameters, this is known as method overloading.
  • When a subclass updates a method of a superclass with the same signature, this is known as method overriding.
  • The method signatures of the overloaded methods must be distinct. It indicates that the methods must vary in at least one of the following: the number of parameters, the kind of parameters, and the order of the parameters, but they must all have the same name.
  • The method signatures of the overridden methods must be the same. It implies that the name of the method, the number of parameters, the order of the arguments, and the type of arguments should all be the same.
  • We don't have any access modifier restrictions, thus we may use any access modifier in the overloaded method.
  • The access modifier for the overridden method in the subclass should be the same or less limited.
  • At compilation time, the method call is bound to the method definition. Static Binding is another name for it.
  • At compilation time, the method call is bound to the method definition. Dynamic Binding is another name for it.

You May Also Like:



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.