Overloading and Overriding

1. Method Overriding

The overriding method is the method of the subclass, which has the same syntax as the overridden method of the superclass. 

The main usages of overriding are:

  • create a specific implementation of a method that is already created by its superclass.
  • runtime polymorphism

Rules of overriding:

  • The overriding method MUST have the same name as in the superclass.
  • The argument list MUST NOT be changed in the overriding method. 
  • The overriding method CANNOT declare a more restrictive access modifier than the overridden method.
  • The overriding method CAN declare less restrictive access than the overridden method.
  • Instance methods can be overridden only if they are inherited by the subclass.
  • The overriding method MUST NOT throw checked exceptions that are new or broader than exceptions declared by the overridden method.
  • The overriding method CAN throw narrower or fewer exceptions.
  • Abstract methods MUST be overridden in the subclass or the subclass should be marked abstract as well.
  • The return type of overriding method MUST be the same or a subtype of the return type of overridden method.
  • The static method CANNOT be overridden.
  • Final methods CANNOT be overridden.
  • Constructors CANNOT be overridden.
  • A subclass should use super.overriddenMethodName() to call the superclass version of an overridden method.
  • Polymorphism applies to overriding, not overloading.
  • Object type (not the reference variable's type) determines which overridden method is used at runtime.

Example 1.1. Method Overriding

The subclass Doll overrides the method printName() of the superclass Toy:

class Toy {
    public void printName() {
        System.out.println("Toy");
    }
}

class Doll extends Toy {
    public void printName() {
        System.out.println("Doll");
    }
}

public class TestToys {
    public static void main(String[] args) {
        Toy toy = new Toy();
        Toy doll = new Doll();
        toy.printName();
        doll.printName();
    }
}

The output is:

Toy
Doll

2. Method Overloading

Overloaded methods give the possibility to reuse the same method name in a class, but with different arguments (and optionally, a different return type). 

Rules for overloading:

  • Overloaded methods MUST change the argument list.
  • Overloaded methods CAN change the return type.
  • Overloaded methods CAN change the access modifier.
  • Overloaded methods CAN declare new or broader checked exceptions.
  • A method can be overloaded in the same class or in a subclass.
  • Constructors can be overloaded.
  • Reference type determines which overloaded method will be used at compile time.

Example 2.1. Overloading Method with Primitives

The method multiply is overloaded to receive different types of numbers:

class Multiplier {
    public int multiply(int x, int y) {
        System.out.println("Multiply int");
        return x * y;
    }

    public double multiply(double x, double y) {
        System.out.println("Multiply double");
        return x * y;
    }
}

public class Test {
    public static void main(String... args) {
        Multiplier m = new Multiplier();
        System.out.print(m.multiply(3, 4));
    }
}

The output is:

Multiply int
12

Example 2.2. Overloading Method with Object References

This example shows how to overload the method with object references:

class Toy {}

class Doll extends Toy {}

public class TestToys {
    public void doJob(Toy toy) {
        System.out.println("Toy version");
    }

    public void doJob(Doll doll) {
        System.out.println("Doll version");
    }

    public static void main(String[] args) {
        TestToys testToys = new TestToys();

        Doll doll = new Doll();
        Toy toy1 = new Toy();
        Toy toy2 = new Doll();

        testToys.doJob(doll);
        testToys.doJob(toy1);
        testToys.doJob(toy2);
    }
}

The output is:

Doll version
Toy version
Toy version
Trustpilot
Trustpilot
Comments