Overloading vs Overriding in Java

Mohit Panthri
3 min readMar 17, 2023

--

Java is an object-oriented programming language that supports both method overloading and method overriding. Both of these concepts are fundamental in Java and are used to achieve polymorphism. Polymorphism is a powerful feature of object-oriented programming that allows you to write code that can work with objects of different types.

In this blog post, we will discuss the differences between method overloading and method overriding in Java.

Method Overloading

Method overloading is a technique in Java where you define two or more methods with the same name in the same class, but with different parameters. The method signature includes the method name and the parameter list. Therefore, in method overloading, the method name is the same, but the parameters are different.

Here is an example of method overloading:

public class Calculator {
public int add(int x, int y) {
return x + y;
}
public int add(int x, int y, int z) {
return x + y + z;
}
public double add(double x, double y) {
return x + y;
}
}

In the above example, we have defined three methods with the name “add”, but with different parameter lists. The first method takes two integer parameters, the second method takes three integer parameters, and the third method takes two double parameters. All three methods have the same name, but they differ in the number and types of their parameters. This is an example of method overloading.

Method Overriding

Method overriding is a technique in Java where a subclass provides a specific implementation of a method that is already provided by its parent class. In method overriding, the method name, return type, and parameter list must be the same as that of the parent class.

Here is an example of method overriding:

public class Animal {
public void sound() {
System.out.println("Animal is making a sound");
}
}
public class Cat extends Animal {
@Override
public void sound() {
System.out.println("Meow");
}
}

In the above example, the Cat class extends the Animal class and overrides the sound() method. The Cat class provides its own implementation of the sound() method, which prints “Meow” to the console. When you call the sound() method on an instance of the Cat class, it will execute the implementation provided by the Cat class, rather than the implementation provided by the Animal class. This is an example of method overriding.

Differences between Method Overloading and Method Overriding

  1. Method overloading is a compile-time polymorphism concept, while method overriding is a runtime polymorphism concept.
  2. In method overloading, the method name is the same, but the parameters are different. In method overriding, the method name, return type, and parameter list must be the same as that of the parent class.
  3. Method overloading is used to provide different ways of calling the same method, while method overriding is used to provide a specific implementation of a method in the subclass.
  4. Method overloading is resolved at compile time, while method overriding is resolved at runtime.
  5. Method overloading is used to add more functionality to a class, while method overriding is used to modify the behavior of a class.

Conclusion

Method overloading and method overriding are two important concepts in Java that are used to achieve polymorphism. Method overloading is used to provide multiple ways of calling the same method, while method overriding is used to provide a specific implementation of a method in the subclass. It is important to understand the differences between these two concepts to use them effectively in your Java programs.

--

--

Mohit Panthri
Mohit Panthri

No responses yet