Inheritance in Java — IS-A Relationship, super Keyword, and Method Overriding
Fast recall
Quick revision
Core answer
extends — only valid when IS-A holds in every context (Liskov Substitution Principle). super(args) chains upward through ancestors; omit it and the compiler inserts super() automatically — missing no-arg constructor = compile error. Override rules: same or wider access, covariant return (child can return Dog where parent returns Animal), narrower checked exceptions. static methods are hidden (reference type decides), not overridden. Prefer composition over inheritance for code reuse.Whether the candidate understands inheritance beyond syntax — can they explain the IS-A test, how super() chaining works, the rules of method overriding, and critically when inheritance is the wrong tool
Using inheritance for code reuse instead of genuine IS-A relationships. Not knowing that super() is called implicitly even when you don't write it. Forgetting that overriding requires same or wider access modifier
Articulate the Liskov Substitution Principle in practical terms — a subclass must be usable anywhere the parent is expected without breaking behavior. Show awareness that composition is usually preferred over inheritance in modern Java
Interview-ready guide
Interview answer
Inheritance lets a child class acquire the fields and methods of a parent through extends. Dog extends Animal — Dog gets Animal's properties and can add or override them. The IS-A relationship is the foundation: a Dog is always an Animal, which means a Dog can stand in anywhere an Animal is expected. That substitutability is what makes runtime polymorphism possible — it's not just code sharing.
IS-A and types
- Three types: single (
Dog extends Animal), multilevel (GoldenRetriever extends Dog extends Animal), hierarchical (Dog,Cat,Cowall extendAnimal) - Multiple via classes is not allowed — diamond problem. Use interfaces instead
- The IS-A test: must hold in every context, not just today's. A Square is mathematically a Rectangle, but a Rectangle caller expects independent width and height — Square breaks that. If the child can't honor the parent's contract, the inheritance is wrong
The super keyword — three uses
super.fieldaccesses a parent field shadowed by a same-name child fieldsuper.method()calls the parent's version of an overridden method — used when extending behavior, not replacing itsuper(args)calls the parent constructor; must be the first line. Omit it and the compiler insertssuper()automatically — causes a compile error if the parent has no no-arg constructor
Method overriding
- Same or wider access:
protectedcan becomepublic, neverprivate - Covariant return: child can return a more specific type (
Dogwhere parent returnsAnimal) - Checked exceptions: only narrower — child can't introduce new ones the caller doesn't handle
- Static methods are hidden, not overridden — reference type decides which runs; always write
@Override
The Liskov Substitution Principle is the interviewer checkpoint. It's the formal name for the IS-A test failing in context — Square-Rectangle is the example everyone expects. Knowing it by name and being able to explain why the code breaks is the answer that separates design-aware from syntax-level.
Inheritance in Java — IS-A Relationship, super Keyword, and Method Overriding
Direct Answer
Inheritance = child acquires parent's properties/methods via extends — only valid when IS-A holds in every context (Liskov Substitution Principle). super(args) chains upward through ancestors; omit it and the compiler inserts super() automatically — missing no-arg constructor = compile error. Override rules: same or wider access, covariant return (child can return Dog where parent returns Animal), narrower checked exceptions. static methods are hidden (reference type decides), not overridden. Prefer composition over inheritance for code reuse.
Common Mistakes & Pitfalls
Using inheritance for code reuse instead of genuine IS-A relationships. Not knowing that super() is called implicitly even when you don't write it. Forgetting that overriding requires same or wider access modifier
Check your understanding
Answer these without looking back at the article.
- What is the Liskov Substitution Principle and how does the Square-Rectangle example violate it?
- Why does Java insert super() automatically if you omit it — what happens if the parent has no no-arg constructor?
- What is the difference between method hiding (static) and method overriding (instance)?
- When should you always use @Override and what silent bug does omitting it cause?
- What is the fragile base class problem and how does it motivate preferring composition?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.