Overloading vs Overriding in Java

Introduction

In this article, we will learn about Overloading and Overriding in Java. In object-oriented programming, overloading and overriding are two important concepts that allow code reusability and polymorphism. While they may sound similar, they have different purposes and have distinct rules.

Method Overloading

Method overloading is a feature that allows a class to have multiple methods with the same name but different parameter lists. The methods must have different types, different numbers of parameters, or different parameter order. The compiler determines which method to call based on the arguments passed during the method invocation.

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 Calculator class has two add() methods with the same name but different parameter types (int and double). The compiler decides which method to call based on the types of arguments provided.

Method Overriding

Method overriding is a mechanism that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The overridden method in the subclass must have the same name, return type, and parameter list as the method in the superclass.

class Vehicle {
    protected void start() {
        System.out.println("Vehicle started.");
    }
}
class Car extends Vehicle {
    @Override
    public void start() {             // Overriding with a access modifier
        System.out.println("Car started.");
    }
}

In the above example, the start() method in the Vehicle class is protected, while in the Car subclass, it is overridden with a public access modifier, which is less restrictive.

Key Differences between Overloading and Overriding

  • Scope: Overloading is a compile-time concept and occurs within a single class while overriding is a run-time concept and occurs between a superclass and its subclass.
  • Parameters: Overloaded methods must have different parameter lists (different types, different numbers of parameters, or different parameter orders), while overridden methods must have the same parameter list as the method in the superclass.
  • Access Modifiers: Overridden methods can have the same or less restrictive access modifiers (e.g., a protected method in the superclass can be overridden as public in the subclass), while overloaded methods can have any access modifier.
  • Return Type: Overloaded methods can have different return types, while overridden methods must have the same return type as the method in the superclass (or a subtype if the method returns an object).
  • Uses: Overloading is used to provide multiple versions of a method with different parameter lists while overriding is used to provide a specialized implementation of a method in a subclass.

Summary 

Both overloading and overriding are essential concepts in object-oriented programming and play crucial roles in code reusability, polymorphism, and inheritance. Understanding the differences between these two concepts is important for writing clear, maintainable, and efficient Java code.


Similar Articles