Java OOP
Inheritance
Complete lesson for Inheritance using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Inheritance
Section: Java OOP. extends, single class inheritance, super, Object, and IS-A vs HAS-A.
IS-A with extends
class Dog extends Animalmeans Dog **is an** Animal. Dog inherits visible fields and methods.- Java allows **one** superclass. Multiple inheritance of *classes* is forbidden (interfaces provide multiple *types*).
- Use inheritance for genuine specialization, not just to reuse a utility method — prefer composition (HAS-A) then.
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void speak() {
System.out.println(name + " makes a sound");
}
}
class Dog extends Animal {
Dog(String name) {
super(name); // first statement
}
@Override
void speak() {
System.out.println(name + " barks");
}
}
public class InheritDemo {
public static void main(String[] args) {
Animal a = new Dog("Rex");
a.speak();
}
} super and constructors
- If you do not write
super(...), the compiler insertssuper(). That fails if the parent has no no-arg constructor. super.method()calls the superclass implementation (useful inside an override).- Private members are not inherited in the usable sense — the subclass cannot access them.
Object and types of inheritance
| Kind | Java support |
|---|---|
| Single class inheritance | Yes — extends one class |
| Multilevel | Yes — C extends B extends A |
| Hierarchical | Yes — many classes extend A |
| Multiple class inheritance | No |
| Multiple interface implementation | Yes — implements I1, I2 |
Advanced — Inheritance
Fragile base class and final
- Overridable methods called from a superclass constructor see a not-yet-initialized subclass — a classic bug. Do not call overridable methods from constructors.
finalclass cannot be extended.finalmethod cannot be overridden.sealedclasses (Java 17) list permitted subclasses.protectedmembers are visible to subclasses (and the same package).- Inheritance copies the API contract. If you only wanted reuse, a private field of that type is simpler (composition).
Tricky Interview Points
Q1) Does Java support multiple inheritance?
Answer Not for classes. A class can implement many interfaces (multiple inheritance of type, and since Java 8 also default method bodies — with conflict rules).
Q2) What is super()?
Answer A call to a superclass constructor. It must be the first statement if present. Implicit super() is inserted otherwise.
Q3) IS-A vs HAS-A?
Answer IS-A is inheritance (Dog is an Animal). HAS-A is composition (Car has an Engine). Prefer HAS-A unless the subtype truly is a kind of the parent.
Q4) Are constructors inherited?
Answer No. The subclass must invoke one parent constructor.
Q5) What does every class extend?
Answer Object, directly or indirectly, unless it is Object itself.
Q6) Can a subclass access private fields of the parent?
Answer No. Use protected/package access or public getters. private is class-private, not hierarchy-private.
Q7) What is multilevel inheritance?
Answer A chain: class C extends B, B extends A. C inherits from both (subject to access).
Simple Programs
Multilevel constructors
class Vehicle {
Vehicle() {
System.out.println("Vehicle");
}
}
class Car extends Vehicle {
Car() {
System.out.println("Car");
}
}
class EvCar extends Car {
EvCar() {
System.out.println("EvCar");
}
public static void main(String[] args) {
new EvCar();
}
} Problem Solving Tricks
- Put
@Overrideon every override so typos become compile errors. - Keep inheritance trees shallow. Deep trees are hard to reason about.
- If you need “is-a” plus extra roles, use interfaces.
- Do not inherit from a class just to get its
staticutilities — import or compose.
Practice Programs List
- Model
Shape→Circle/Rectanglewith aarea()method. - Give
Shapeonly a parameterized constructor and fix subclass constructors withsuper. - Print the constructor order of a three-level hierarchy.
- Replace a bad “Manager extends ArrayList” design with composition.
- Mark a method
finaland try to override it — read the error.