Inheritance

Thinking

Inheritance is a fundamental concept in Java that enables new classes to acquire properties and behaviors of existing classes, promoting code reuse and logical hierarchy.

Understanding different types of inheritance — single, multilevel, and hierarchical — is essential to design robust and maintainable Java applications.

Description

Inheritance allows a class (child/subclass) to inherit fields and methods from another class (parent/superclass).

  • Single Inheritance: A subclass inherits from one superclass.
    Example: class Dog extends Animal {}
  • Multilevel Inheritance: A class inherits from a subclass, forming a chain.
    Example: class Puppy extends Dog extends Animal {}
  • Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
    Example: class Dog extends Animal {} and class Cat extends Animal {}

Java supports single, multilevel, and hierarchical inheritance but does not support multiple inheritance with classes to avoid ambiguity.

Video Resources

Java Inheritance Explained

A clear explanation of inheritance types in Java.

Examples (code)

Single Inheritance Example


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

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 method
        d.bark();
    }
}
  

Multilevel Inheritance Example


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

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

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

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

Hierarchical Inheritance Example


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

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

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

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.bark();

        Cat cat = new Cat();
        cat.eat();
        cat.meow();
    }
}
  

Real-World Applications

Code Reusability

Inheritance allows reuse of existing code by extending classes instead of rewriting common functionality.

Hierarchical Class Structure

Helps organize classes in parent-child relationships to model real-world entities and systems.

Framework Design

Inheritance is extensively used in frameworks and APIs for creating extendable and modular software components.

Interview Questions

Q1: What types of inheritance does Java support?

Show Answer

Java supports single, multilevel, and hierarchical inheritance but does not support multiple inheritance with classes.

Q2: Why doesn't Java support multiple inheritance using classes?

Show Answer

To avoid ambiguity caused by the diamond problem, where multiple parent classes have methods with the same signature, leading to confusion.

Q3: How can you achieve multiple inheritance in Java?

Show Answer

Multiple inheritance can be achieved using interfaces, which allow a class to implement multiple interfaces.