Inheritance in Java: extends Keyword and Method Overriding Explained with Examples

Illustration for Inheritance in Java: extends Keyword and Method Overriding Explained with Examples
By Last updated:

Inheritance is one of the core pillars of Object-Oriented Programming (OOP) in Java. It allows you to create new classes based on existing ones, promoting code reuse and polymorphism.


📌 What is Inheritance?

  • Definition: Inheritance is a mechanism where one class (child/subclass) acquires the properties and behaviors of another class (parent/superclass).
  • Why it matters: Reduces code duplication, improves maintainability, and enables polymorphic behavior.
  • When to use: When multiple classes share common behavior or structure.

[Related: link-to-other-article]


🔹 The extends Keyword

  • Used to define a subclass that inherits from a superclass.
  • Java supports single inheritance (one parent class).

💻 Example:

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

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

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound(); // Inherited from Animal
        d.bark();  // Dog's own method
    }
}

🔹 Method Overriding

  • Definition: When a subclass provides its own implementation of a method already defined in the parent class.
  • Rules:
    • Method name and parameters must match exactly.
    • Access modifier cannot be more restrictive.
    • Use @Override annotation to avoid mistakes.

💻 Example:

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

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

🔹 Real-World Analogy

Think of inheritance like a family tree. A child inherits traits from parents (fields/methods) but can also override certain behaviors (method overriding).


🚫 Common Mistakes and Anti-Patterns

  • ❌ Using inheritance for code reuse without proper IS-A relationship.
  • ❌ Forgetting to use @Override, leading to method overloading instead of overriding.
  • ❌ Overusing inheritance instead of composition.

📈 Performance and Memory Implications

  • Slight overhead in method lookups for overridden methods due to dynamic binding.
  • Reduces code duplication, saving memory in large projects.
Aspect Impact
Inheritance Promotes code reuse
Method Overriding Enables runtime polymorphism

🔧 Best Practices

  • Use @Override for clarity and safety.
  • Favor composition over inheritance when classes don’t share a true IS-A relationship.
  • Keep inheritance hierarchies shallow to maintain readability.

📚 Interview Questions

  1. Q: Can constructors be inherited?
    A: No, constructors are not inherited but can be called via super().

  2. Q: What’s the difference between method overloading and overriding?
    A: Overloading is compile-time polymorphism; overriding is runtime polymorphism.

  3. Q: Can a subclass override a private method?
    A: No, private methods are not visible to subclasses.


📌 Java Version Relevance

Java Version Change
Java 1.0 Inheritance and extends introduced
Java 5 @Override annotation introduced

✅ Conclusion & Key Takeaways

  • Inheritance enables code reuse and polymorphism.
  • Use extends for subclassing and @Override to customize behavior.
  • Avoid deep hierarchies and use composition when more appropriate.

❓ FAQ

Q: Does Java support multiple inheritance?
A: No, but you can achieve similar behavior using interfaces.

Q: Can we prevent a class from being inherited?
A: Yes, by declaring it final.

Q: Can static methods be overridden?
A: No, they are hidden, not overridden.