Skip to main content

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.

L1

Basics — Inheritance

Section: Java OOP. extends, single class inheritance, super, Object, and IS-A vs HAS-A.

IS-A with extends

  • class Dog extends Animal means 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 inserts super(). 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

KindJava support
Single class inheritanceYes — extends one class
MultilevelYes — C extends B extends A
HierarchicalYes — many classes extend A
Multiple class inheritanceNo
Multiple interface implementationYes — implements I1, I2
L2

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.
  • final class cannot be extended. final method cannot be overridden. sealed classes (Java 17) list permitted subclasses.
  • protected members 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).
L3

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).

L4

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();
    }
}
L5

Problem Solving Tricks

  • Put @Override on 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 static utilities — import or compose.
L6

Practice Programs List

  1. Model Shape → Circle / Rectangle with a area() method.
  2. Give Shape only a parameterized constructor and fix subclass constructors with super.
  3. Print the constructor order of a three-level hierarchy.
  4. Replace a bad “Manager extends ArrayList” design with composition.
  5. Mark a method final and try to override it — read the error.