Java Method Overriding : Complete Guide with Examples

Method overriding is a core concept in Java, allowing a subclass to provide a specific implementation for a method already defined in its superclass. It plays a crucial role in achieving runtime polymorphism.


1. What is Method Overriding?

Method overriding occurs when a subclass defines a method with the same name, return type, and parameters as a method in its superclass.

Key Points:

  • Overridden method must have same name, return type, and parameters.
  • Access level cannot be more restrictive than the superclass method.
  • Only instance methods can be overridden (static, final, and private methods cannot be overridden).
  • Supports runtime polymorphism.

2. Method Overriding Syntax

class SuperClass {
    void display() {
        System.out.println("Display from SuperClass");
    }
}

class SubClass extends SuperClass {
    @Override
    void display() {
        System.out.println("Display from SubClass");
    }
}

public class Test {
    public static void main(String[] args) {
        SuperClass obj = new SubClass();
        obj.display();  // Calls SubClass's display()
    }
}

Explanation:

  • @Override is optional but recommended. It ensures you are actually overriding a method.
  • The method in SubClass replaces the SuperClass version when called on a subclass object.

3. Rules of Method Overriding

RuleExplanationExample
Method signature must be sameName + parameters must matchvoid display() cannot override void show()
Return type must be compatibleCan be same or a subtype (covariant return type)String getName() in subclass can override Object getName() in superclass
Access levelCannot be more restrictiveprotected in superclass → public in subclass ✅; privateprotected
Final method cannot be overriddenfinal void print() cannot be overriddenCompile-time error
Static methods cannot be overriddenStatic methods are hidden, not overriddenstatic void show() in subclass hides superclass method
Private methods cannot be overriddenThey are not inheritedprivate void msg() cannot be overridden
Constructor cannot be overriddenConstructors are not inherited

4. Covariant Return Type

Java allows overriding methods to return a subclass type of the original method’s return type.

class SuperClass {
    SuperClass getInstance() {
        return new SuperClass();
    }
}

class SubClass extends SuperClass {
    @Override
    SubClass getInstance() {
        return new SubClass();
    }
}

Explanation:
SubClass getInstance() overrides SuperClass getInstance() because SubClass is a subtype of SuperClass.

5. Access Modifier Scenarios

  • Superclass method is private → cannot override.
  • Superclass method is default/package-private → can override in the same package.
  • Superclass method is protected → can override with protected or public.
  • Superclass method is public → can override only with public.

6. Final Method Scenario

class SuperClass {
    final void show() {
        System.out.println("Final method");
    }
}

class SubClass extends SuperClass {
    // void show() { } // ❌ Compile-time error
}

Explanation: Final methods cannot be overridden.

7. Static Method Hiding

Static methods cannot be overridden; they are hidden.

class SuperClass {
    static void greet() {
        System.out.println("Hello from SuperClass");
    }
}

class SubClass extends SuperClass {
    static void greet() {
        System.out.println("Hello from SubClass");
    }
}

public class Test {
    public static void main(String[] args) {
        SuperClass.greet(); // SuperClass version
        SubClass.greet();   // SubClass version
    }
}

Explanation: This is method hiding, not overriding. The version called depends on the class reference, not the object.

8. Real-Time Example: Overriding in a Banking System

class Bank {
    double getInterestRate() {
        return 5.0;
    }
}

class SBI extends Bank {
    @Override
    double getInterestRate() {
        return 6.5;
    }
}

class ICICI extends Bank {
    @Override
    double getInterestRate() {
        return 7.0;
    }
}

public class TestBank {
    public static void main(String[] args) {
        Bank b1 = new SBI();
        Bank b2 = new ICICI();
        System.out.println("SBI Interest: " + b1.getInterestRate());
        System.out.println("ICICI Interest: " + b2.getInterestRate());
    }
}

Explanation:

  • Runtime polymorphism allows different bank types to provide their own interest rates.
  • The same method getInterestRate() behaves differently depending on the subclass object.

9. Super Keyword Usage in Overriding

You can call the parent class method from the subclass using super.

class SuperClass {
    void display() {
        System.out.println("SuperClass method");
    }
}

class SubClass extends SuperClass {
    @Override
    void display() {
        super.display(); // Calls SuperClass method
        System.out.println("SubClass method");
    }
}

Output:

SuperClass method
SubClass method

10. Summary

  • Overriding allows dynamic polymorphism.
  • Must follow method signature, access modifier, and return type rules.
  • final, static, private methods cannot be overridden.
  • super keyword helps access parent class methods.
  • Real-time use: frameworks (Spring, Hibernate), banking systems, UI, and game engines.

💡 Tip for Readers:
Always use @Override annotation. It helps avoid errors like accidental overloading instead of overriding.

Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

Leave a Reply

Your email address will not be published. Required fields are marked *