Types of Inheritance

Naveen AnamaneniUpdated · 4 min read

Family trees come in different shapes: a straight line from grandparent to parent to child, or one parent with several children. Class hierarchies have shapes too, and interviewers often ask you to name them. Java supports three shapes with classes, and handles the other two (multiple and hybrid) with interfaces instead.

The Shapes

Three shapes with classes; multiple and hybrid only through interfaces
TypeShapeExampleClasses in Java?
SingleOne parent, one childManager extends EmployeeYes
MultilevelA chainDirector extends Manager extends EmployeeYes
HierarchicalOne parent, many childrenManager, Developer, Tester all extend EmployeeYes
MultipleOne child, several parentsclass C extends A, BNo (use interfaces)
HybridA mix that includes multipleNo for classes; possible with interfaces

Multilevel and Hierarchical Together

Company.java▶ Try it
public class Company {
    static class Employee {
        String name;
        Employee(String name) { this.name = name; }
        String role() { return "Employee"; }
    }
 
    static class Manager extends Employee {
        int teamSize;
        Manager(String name, int size) { super(name); teamSize = size; }
        @Override
        String role() { return "Manager of " + teamSize; }
    }
 
    static class Director extends Manager {
        Director(String name, int teamSize) { super(name, teamSize); }
        @Override
        String role() { return "Director, " + super.role(); }
    }
 
    static class Developer extends Employee {
        Developer(String name) { super(name); }
    }
 
    public static void main(String[] args) {
        Employee[] staff = {
            new Developer("Srihitha"),
            new Manager("Advitha", 6),
            new Director("Navika", 40)
        };
        for (Employee e : staff) {
            System.out.println(e.name + ": " + e.role());
        }
    }
}
Output
Srihitha: Employee
Advitha: Manager of 6
Navika: Director, Manager of 40

How it works

  1. Director extends Manager extends Employee is multilevel: a chain of three.
  2. Manager and Developer both extend Employee: hierarchical.
  3. Each class can override role() (marked @Override); Director reuses its parent's version with super.role().
  4. One array of type Employee[] holds all three kinds, and each object answers role() in its own way. That's polymorphism, covered in "Polymorphism in Java".

Try it yourself: add Tester extends Employee with its own role(), and add a tester to the array.

Why Not Multiple Inheritance?

class C extends A, B doesn't even parse:

Compiler output
Multi.java:2: error: '{' expected
class C extends A, B {}
                 ^

If A and B both had a method greet(), which one would C inherit? That ambiguity is the diamond problem, explained in the next lesson. Java avoids it for classes and allows multiple inheritance of type through interfaces: class C extends A implements X, Y.

Common Mistakes

Trying extends A, B. Use one parent class plus interfaces.

Deep chains for small differences. A field or a strategy object is often simpler than another subclass.

Calling hierarchical inheritance "multiple". Several children of one parent is hierarchical; one child of several parents is multiple.

Interview Corner

What types of inheritance does Java support?

With classes: single, multilevel and hierarchical. Multiple inheritance of classes isn't allowed, to avoid the diamond problem, so hybrid inheritance that includes it isn't either. A class can implement several interfaces, which gives multiple inheritance of type and, through default methods, of behavior.

Practice

  1. Easy: Name the type: Car extends Vehicle; Bike extends Vehicle and Bus extends Vehicle; ElectricCar extends Car extends Vehicle.
  2. Harder: Model Shape with Circle and Rectangle, and Square extends Rectangle. Give each an area() method and print the areas from one Shape[] array.

Quick check: Types of inheritance

4 questions
1Director extends Manager extends Employee is which type?
2Several classes extending one parent is called:
3Can a Java class extend two classes?
4How does Java offer multiple inheritance of type?

Key Takeaways

Was this tutorial helpful?

Lesson 113 of 482