What are the types of inheritance, and which does Java support through classes?
Single, multilevel, hierarchical, multiple and hybrid. Java supports the first three with classes; multiple inheritance of classes is not allowed, but a class can implement many interfaces.
Explain it simply
Think of a family tree. Single: a child has one parent class. Multilevel: grandparent, parent, child. Hierarchical: one parent with several children. Multiple: a child with two parents, which Java forbids for classes. Hybrid mixes several of these.
Java skips multiple class inheritance to avoid confusion about which parent's version to use. Interfaces fill the gap, because they describe abilities and do not carry state.
Example
class Vehicle { void start() { System.out.println("starting"); } }
class Car extends Vehicle { } // single
class ElectricCar extends Car { } // multilevel (Vehicle > Car > ElectricCar)
class Bike extends Vehicle { } // hierarchical (Car and Bike share Vehicle)
interface Chargeable { void charge(); }
interface Connected { void connect(); }
class SmartCar extends Car implements Chargeable, Connected { // many abilities
public void charge() { }
public void connect() { }
}Python and C++ do allow multiple inheritance of classes, with rules such as MRO to decide the order.
What to say in the interview
"Common types are single, multilevel, hierarchical, multiple and hybrid. Java supports single, multilevel and hierarchical inheritance for classes, and gets multiple inheritance of type through interfaces to avoid the diamond problem."
Common mistakes
- Saying Java does not support multiple inheritance at all. It does, through interfaces.