Inheritance and why Java forbids multiple class inheritance, composition over inheritance, association vs aggregation vs composition, IS-A vs HAS-A, and the this and super keywords.
Published September 25, 2026
Interviewers use inheritance questions to find out whether you'll build rigid class hierarchies or flexible designs. Show that you know the mechanics (extends, super), and when not to use inheritance.
Short answer: Inheritance lets a class (the subclass) acquire the fields and methods of another class (the superclass) using extends. The subclass reuses and specialises that behaviour, and can override methods.
class Vehicle {
protected int speed;
void accelerate(int delta) { speed += delta; }
}
class Car extends Vehicle { // Car IS-A Vehicle
int doors = 4;
@Override void accelerate(int delta) { speed += Math.min(delta, 20); } // specialised behaviour
}
Key points to cover:
Object.Learn it in depth → Inheritance and Polymorphism
Short answer: Code reuse, and above all substitutability: a Car can be used anywhere a Vehicle is expected. That's what makes polymorphism possible.
Key points to cover:
Square extends Rectangle, because setting the width independently breaks the square.Learn it in depth → Liskov Substitution & Interface Segregation
Short answer: No. class A extends A {} is a compile error ("cyclic inheritance involving A"). The same applies to longer cycles, such as A extends B with B extends A.
Short answer: To avoid the diamond problem. If C extended both A and B, and both defined greet(), it would be ambiguous which one C inherits. There's also the harder question of how to combine two sets of inherited state. Java keeps class inheritance single, and gets multiple inheritance of type through interfaces.
Key points to cover:
default methods, so a similar conflict can arise. Java resolves it by forcing the class to override the method, and optionally choose one version with A.super.greet().Learn it in depth → Interfaces and Abstract Classes
Short answer: Inheritance is an IS-A relationship: the subclass is a specialised parent. Composition is a HAS-A relationship: a class contains other objects and delegates work to them. "Composition over inheritance" means preferring to combine objects, because it's more flexible and less coupled.
// Inheritance misuse: a Car is not an Engine
// class Car extends Engine { }
// Composition: Car HAS-A Engine, and the engine can be swapped
class Car {
private final Engine engine; // injected, so easy to replace or mock
Car(Engine engine) { this.engine = engine; }
void start() { engine.ignite(); }
}
interface Engine { void ignite(); }
class PetrolEngine implements Engine { public void ignite() { /* … */ } }
class ElectricEngine implements Engine { public void ignite() { /* … */ } }
Key points to cover:
Learn it in depth → Strategy Pattern
Short answer: All three describe how objects are related, from loosest to tightest:
Teacher teaches Students, and both live independently.Department has Professors, who remain if the department closes.Order has OrderLines, which make no sense without the order.Key points to cover:
Short answer: IS-A is inheritance or interface implementation: Car extends Vehicle, so a Car IS-A Vehicle. HAS-A is composition: Car has an Engine field, so a Car HAS-A Engine.
Key points to cover:
instanceof checks IS-A relationships at runtime.this and super keywords?Short answer: this refers to the current object. super refers to the parent-class part of the current object. It's used to call the parent's constructor, or its version of an overridden method.
class Employee {
protected String name;
Employee(String name) { this.name = name; } // this.name = the field, name = the parameter
String describe() { return "Employee " + name; }
}
class Manager extends Employee {
private final int reports;
Manager(String name, int reports) {
super(name); // must be the first statement in the constructor
this.reports = reports;
}
@Override String describe() {
return super.describe() + " managing " + reports; // reuse the parent's version
}
}
Key points to cover:
this:
this(...), known as constructor chaining.this for fluent APIs.this(...) and super(...) must be the first statement, so a constructor can't contain both. (Java 25's "flexible constructor bodies" allow statements that don't use this before the call.)Learn it in depth → Inheritance and Polymorphism
this be reassigned? What happens if you use super in a class with no explicit parent?Short answer: this is effectively final. this = other; is a compile error. And every class except Object has a superclass (Object by default), so super.toString() or super() compiles fine in a class that doesn't write extends.
Common trap: some answer keys claim that using super in a class without a parent is a compile error. It isn't: the implicit parent is Object. The only place super has nothing to refer to is inside java.lang.Object itself.
this or super be used in a static method?Short answer: No. Static methods belong to the class, not to any object, so there's no current instance for this or super to refer to. Using them is a compile error ("non-static variable this cannot be referenced from a static context").
Key points to cover:
static main, create an object first: new App().run().super relate to polymorphism?Short answer: Polymorphism lets an overriding method replace the parent's behaviour. super.method() lets the override extend that behaviour instead of replacing it: run the parent logic, then add to it. At runtime, calls through a parent-type reference still dispatch to the subclass's override, and super is how that override reaches the original.
Key points to cover:
toString()/equals() overrides that call super.super.method() is bound statically to the parent's implementation. It doesn't go through dynamic dispatch.Learn it in depth → Template Method Pattern
Q: Are constructors inherited?
A: No. Each class declares its own constructors. If a subclass constructor doesn't call super(...) explicitly, the compiler inserts super(). That fails to compile if the parent has no no-argument constructor.
Q: Are private members inherited? A: The private fields exist in the subclass object's memory, but the subclass can't access them directly. It has to go through protected or public methods. Private methods can't be overridden.
Q: What order do constructors run in, in a class hierarchy?
A: From the top down. Object's constructor runs first, then each parent's, then the subclass's. Static initialisers run once, when each class is loaded, also from parent to child.
Q: How can you stop a class from being inherited?
A: Declare it final (like String), give it only private constructors, or (since Java 17) make it sealed and list exactly which classes may extend it.