🧬 Java Inheritance - Complete Notes

1. What is Inheritance in Java?

Definition: Inheritance is the process where one class acquires properties and behaviors (fields and methods) of another class.

Key Points:

Real-life Example:

👨 Father has a car and house
👦 Son also gets the car and house from father → this is inheritance

Java Example:

// Parent class
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Child class
class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();  // inherited from Animal
        d.bark(); // from Dog
    }
}

2. Types of Inheritance

Types of Inheritance
Type Java Support Description
Single Inheritance ✅ Yes One class inherits from another class
Multilevel ✅ Yes Class inherits from a class which itself inherits another
Hierarchical ✅ Yes Multiple classes inherit from one parent
Multiple ❌ No One class inherits from more than one class
Hybrid ❌ No (but possible with interfaces) Mix of above types

Multilevel Inheritance Example:

class Animal {
    void sound() {
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

class Puppy extends Dog {
    void weep() {
        System.out.println("Puppy weeps");
    }
}

Hierarchical Inheritance Example:

class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking...");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("Meowing...");
    }
}

3. Why Multiple Inheritance is Not Allowed in Java?

Reason: Diamond Problem
Diamond Problem When two parent classes have a method with the same name, and the child class inherits both, Java doesn't know which method to call. This creates ambiguity.

Problem Example:

class A {
    void show() { System.out.println("A"); }
}

class B {
    void show() { System.out.println("B"); }
}

// class C extends A, B ❌ ERROR: Java doesn't allow this

4. How to Achieve Multiple Inheritance in Java?

Solution: By using Interfaces!
Java allows implementing multiple interfaces because interfaces don't contain implementation (just method signatures), so no conflict occurs.

Example:

interface Printable {
    void print();
}

interface Showable {
    void show();
}

class Demo implements Printable, Showable {
    public void print() {
        System.out.println("Printing...");
    }
    public void show() {
        System.out.println("Showing...");
    }
}

7. Interview Questions & Answers

Question Sample Answer
What is inheritance? Acquiring properties and methods of one class into another using extends.
What types of inheritance are supported in Java? Single, Multilevel, Hierarchical. Not Multiple (with classes).
Why doesn't Java support multiple inheritance with classes? To avoid ambiguity caused by the Diamond Problem.
How does Java support multiple inheritance? By using interfaces.
What is method overriding? Redefining a parent class method in child class.
Difference between overloading and overriding? Overloading = same method name, different parameters (compile-time).
Overriding = same method name/signature, different classes (runtime).
What is early and late binding? Early = compile-time (overloading), Late = runtime (overriding).
What is the use of super keyword? To call parent class method or constructor.