Fundamentals 5 min read

Overloads vs Overrides in Java: When and How They Differ

This article explains the distinction between method overloading and method overriding in Java, covering their definitions, how the compiler or runtime selects the appropriate method, differences in method signatures, return types, and timing, and provides clear code examples for each concept.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Overloads vs Overrides in Java: When and How They Differ

Method Overloading

Overloading means defining multiple methods with the same name inside a single class but with different parameter lists – different types, numbers, or order of parameters. The compiler decides which method to call based on the argument types at compile time.

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 example above, the add method is overloaded: one version works with int arguments, the other with double arguments. Calls such as new Calculator().add(1, 2) invoke the integer version, while new Calculator().add(1.5, 2.5) invoke the double version.

Method Overriding

Overriding occurs when a subclass provides its own implementation of a method that already exists in its superclass. The method signature (name, parameter list, and return type) must be exactly the same, and the subclass method replaces the superclass method at runtime, enabling polymorphism.

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

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

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows");
    }
}

When a Dog or Cat object is referenced as an Animal, calling makeSound() executes the overridden method in the actual subclass, demonstrating runtime polymorphism.

Differences in Method Signatures

For overloading, the method signatures must differ – the parameter list must be distinct. The return type may be the same or different.

For overriding, the method signature must be identical to the one in the superclass, including the return type (or a covariant return type, i.e., a subclass of the original return type).

Return‑Type Rules

Overloaded methods can have different return types, but the compiler still distinguishes them based on the parameter list.

Overridden methods must return the same type as the superclass method, or a subtype (covariant return).

Timing of Selection

Overloading is resolved at compile time: the compiler selects the appropriate method based on the static types of the arguments.

Overriding is resolved at runtime: the JVM selects the method implementation based on the actual object type, enabling dynamic dispatch.

JavaOOPPolymorphismMethod OverloadingMethod Overriding
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.