Understanding Overriding vs Overloading in Java

Introduction

link to this section

In Java, both method overriding and method overloading are essential concepts used to achieve polymorphism, which allows methods to behave differently based on their parameters or object types. While they sound similar, they have distinct purposes and usage scenarios. In this tutorial, we'll explore the differences between method overriding and method overloading in Java.

Method Overloading

link to this section

Method overloading refers to the ability to define multiple methods in the same class with the same name but different parameter lists. This allows methods to perform similar operations on different types of input parameters.

Example of Method Overloading

public class Calculator { 
    public int add(int a, int b) { 
        return a + b; 
    } 
    
    public double add(double a, double b) { 
        return a + b; 
    } 
} 

In the above example, the add method is overloaded with two versions: one that accepts two integers and another that accepts two doubles. The compiler differentiates between them based on the parameter types.

Method Overriding

link to this section

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. This allows a subclass to provide its own implementation of a method inherited from its superclass, enabling polymorphic behavior.

Example of Method Overriding

class Animal { 
    void sound() { 
        System.out.println("Animal makes a sound"); 
    } 
} 

class Dog extends Animal { 
    @Override 
    void sound() { 
        System.out.println("Dog barks"); 
    } 
} 

In the above example, the sound method is overridden in the Dog class to provide a specific implementation for the sound made by a dog.

Key Differences

link to this section

1. Purpose

  • Overloading : Used to define multiple methods with the same name but different parameter lists within the same class.
  • Overriding : Used to provide a specific implementation of a method that is already defined in a superclass within a subclass.

2. Inheritance

  • Overloading : Not related to inheritance; methods are defined within the same class.
  • Overriding : Relies on inheritance; the method to be overridden must be inherited from a superclass.

3. Signature

  • Overloading : Methods have the same name but different parameter lists (different number, type, or order of parameters).
  • Overriding : Methods have the same name, return type, and parameter list.

4. Usage

  • Overloading : Used for providing multiple ways to call a method with different input parameters.
  • Overriding : Used to provide specific behavior in subclasses, enabling polymorphic behavior.

Conclusion

link to this section

In summary, method overloading and method overriding are both important features of Java that enable polymorphism and code reuse. While method overloading allows multiple methods with the same name but different parameter lists within the same class, method overriding allows subclasses to provide their own implementations of methods inherited from a superclass. Understanding the differences between the two concepts is crucial for writing clear and maintainable code in Java.