← Back to Home

Method Overriding

Method overriding is an OOP feature in Java where a child class provides its own implementation of a method that is already defined in its parent class. It enables runtime polymorphism, allowing Java to decide which method to execute at runtime based on the object.

This is a very high-frequency interview topic.

What Is Method Overriding?

  • Same method name
  • Same parameter list
  • Same return type (or covariant)
  • Occurs between parent and child classes
  • Resolved at runtime
class Parent {
    void show() {
        System.out.println("Parent show");
    }
}
class Child extends Parent {
    void show() {
        System.out.println("Child show");
    }
}
          

Why Method Overriding Is Needed

  • Enables runtime polymorphism
  • Allows custom behavior in child classes
  • Supports extensibility
  • Promotes flexible design

Basic Example

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

Animal a = new Dog();
a.sound();  // Dog barks
          

✔ Method call depends on object type, not reference type.

Rules of Method Overriding (Very Important)

1. Method Signature Must Be Same

void display()
          

✔ Name + parameters must match exactly.

2. Access Modifier Cannot Be More Restrictive

class A {
    protected void show() { }
}
class B extends A {
    public void show() { } // ✔ allowed
}
          

❌ This is invalid: protected → private

3. Return Type Must Be Same or Covariant

class A {
    A getObject() { }
}
class B extends A {
    B getObject() { } // ✔ covariant return type
}
          

4. Static Methods Cannot Be Overridden

static void test() { }
          

✔ Static methods are hidden, not overridden.

5. final Methods Cannot Be Overridden

final void display() { }
          

❌ Compile-time error if overridden.

6. Constructors Cannot Be Overridden

  • Constructors belong to the class itself
  • Not inherited

7. Exception Rules

  • Overriding method cannot throw broader checked exceptions
  • Can throw same or subclass exception

@Override Annotation (Best Practice)

@Override
void show() { }
          

Benefits:

  • Compile-time checking
  • Prevents signature mistakes
  • Improves readability

Method Overriding vs Method Overloading

Feature Overriding Overloading
Occurs In Inheritance Same class
Parameters Same Different
Return Type Same / covariant Any
Binding Runtime Compile-time
Polymorphism Runtime Compile-time

Runtime Polymorphism Explained

Parent p = new Child();
p.show();
          
  • JVM decides method execution at runtime
  • Based on actual object, not reference

Using super in Overriding

class Child extends Parent {
    void show() {
        super.show();
        System.out.println("Child logic");
    }
}
          

✔ Extends parent behavior instead of replacing it.

Common Beginner Mistakes

  • Changing parameter list (becomes overloading)
  • Forgetting inheritance
  • Overriding static methods
  • Using more restrictive access modifier
  • Not using @Override

Interview-Ready Answers

Short Answer

Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class.

Detailed Answer

In Java, method overriding occurs when a subclass defines a method with the same signature and return type as a parent class method. The method call is resolved at runtime based on the object type, enabling runtime polymorphism.

Key Takeaway

Method overriding enables dynamic behavior in Java. It is the core mechanism behind runtime polymorphism, making Java applications flexible and extensible.