All topics
library
beginner

Inheritance & Method Resolution

Understand how Java resolves which method to call when classes form an inheritance hierarchy.

Inheritance lets a subclass acquire fields and methods of a superclass, enabling code reuse and polymorphic behavior. Java supports single class inheritance but allows implementing multiple interfaces.

A family recipe: the child inherits grandma's recipe but can modify it (override). When you ask the child to cook, they use their version.

Key Concepts

1
Method resolution follows a strict order: the JVM first looks in the actual runtime class, then walks up the chain. For overridden methods, the runtime type determines which version runs (dynamic dispatch). For overloaded methods, the compile-time type determines which signature is selected (static dispatch). This distinction is a perennial interview question.
2
Constructor chaining: every constructor must call a superclass constructor (explicitly via super(...) or implicitly via no-arg super()). If the superclass lacks a no-arg constructor, the subclass must call super(...) explicitly.
3
The super keyword lets you call the parent's overridden method. The final keyword on a method prevents overriding; on a class, prevents inheritance entirely.
4
Deep hierarchies (>3 levels) create tight coupling and fragile base class problems. Prefer composition over inheritance when the relationship isn't truly is-a.