Types of Inheritance
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
| Type | Shape | Example | Classes in Java? |
|---|---|---|---|
| Single | One parent, one child | Manager extends Employee | Yes |
| Multilevel | A chain | Director extends Manager extends Employee | Yes |
| Hierarchical | One parent, many children | Manager, Developer, Tester all extend Employee | Yes |
| Multiple | One child, several parents | class C extends A, B | No (use interfaces) |
| Hybrid | A mix that includes multiple | No for classes; possible with interfaces |
Multilevel and Hierarchical Together
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());
}
}
}Srihitha: Employee
Advitha: Manager of 6
Navika: Director, Manager of 40How it works
Director extends Manager extends Employeeis multilevel: a chain of three.ManagerandDeveloperboth extendEmployee: hierarchical.- Each class can override
role()(marked@Override);Directorreuses its parent's version withsuper.role(). - One array of type
Employee[]holds all three kinds, and each object answersrole()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:
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
- Easy: Name the type:
Car extends Vehicle;Bike extends VehicleandBus extends Vehicle;ElectricCar extends Car extends Vehicle. - Harder: Model
ShapewithCircleandRectangle, andSquare extends Rectangle. Give each anarea()method and print the areas from oneShape[]array.
Key Takeaways
Was this tutorial helpful?