Types of Inheritance
The types of inheritance are five structural arrangements of base and derived classes: single (one subclass, one superclass), multilevel (a chain of classes), hierarchical (many subclasses of one superclass), multiple (one subclass with several superclasses) and hybrid (a mix of these). C++ supports multiple inheritance of classes, while Java and C# allow it only through interfaces.
Inheritance isn't just one shape. Depending on how classes are arranged relative to each other, it falls into one of five recognized categories.
The five types
Single inheritance: one subclass inherits from exactly one superclass. This is the simplest and most common case.
Animal → Dog
Multilevel inheritance: a chain, where a subclass itself becomes the superclass for another class.
Animal → Mammal → Dog
Hierarchical inheritance: multiple subclasses inherit from the same single superclass.
Animal
/ \
Dog Cat
Multiple inheritance: one subclass inherits from more than one superclass at the same time. C++ supports this directly; Java and C# don't allow it for classes (only through interfaces), largely because of the ambiguity problems it can create.
Flyable Swimmable
\ /
Duck
Hybrid inheritance: a combination of two or more of the above types in the same hierarchy, most commonly hierarchical mixed with multiple.
A worked example
#include <iostream>
using namespace std;
class Animal {
public:
void eat() { cout << "This animal eats food." << endl; }
};
class Mammal : public Animal { // multilevel: Mammal inherits Animal
public:
void breathe() { cout << "This mammal breathes air." << endl; }
};
class Dog : public Mammal { // Dog inherits Mammal, which inherited Animal
public:
void bark() { cout << "Woof!" << endl; }
};
int main() {
Dog d;
d.eat(); // from Animal, two levels up
d.breathe(); // from Mammal, one level up
d.bark(); // Dog's own
return 0;
}
Dog here demonstrates multilevel inheritance: it doesn't inherit from Animal directly, it inherits from Mammal, which inherits from Animal. All three levels' methods are available on a Dog object.
Where this matters in real systems
Hierarchical inheritance is everywhere in UI toolkits: a single base Shape class might be the superclass for Circle, Rectangle, and Triangle, each specializing the same shared interface (like a draw() method) differently. Multiple inheritance through interfaces shows up constantly in Java: a class like ArrayList implements both List and RandomAccess, combining capabilities from two separate interface hierarchies without the ambiguity problems that multiple class inheritance can cause.
What interviewers actually ask about types of inheritance
- "Why doesn't Java support multiple inheritance of classes?" To avoid the diamond problem, the ambiguity that arises when a class inherits the same member from two different paths. Java sidesteps it by only allowing multiple inheritance through interfaces, which don't carry implementation ambiguity in the same way.
- "What's the difference between hierarchical and multilevel inheritance?" Hierarchical is one superclass with multiple direct subclasses; multilevel is a chain where each class extends the one before it.
- "Give an example of hybrid inheritance." Any design mixing two shapes, for example a hierarchy where
Animalhas subclassesBirdandFish(hierarchical), andBirdfurther has its own subclassPenguin(multilevel) layered on top. - "Does C++ allow multiple inheritance, and what's the risk?" Yes, directly. The risk is the diamond problem when two base classes share a common ancestor, which is solved with virtual base classes.
Key takeaways
- The five types are single, multilevel, hierarchical, multiple, and hybrid.
- Single: one base, one derived. Multilevel: a chain. Hierarchical: one base, many derived.
- Multiple: one derived class, several base classes at once, supported directly in C++ but only via interfaces in Java/C#.
- Hybrid combines two or more of the other types in one hierarchy.
More in Inheritance
Browse all OOP topics
Log in to save which topics you've read across every subject.
Frequently asked questions
Why doesn't Java support multiple inheritance of classes?
To avoid the diamond problem, the ambiguity that arises when a class inherits the same member from two different paths. Java sidesteps it by only allowing multiple inheritance through interfaces, which don't carry implementation ambiguity in the same way.
What's the difference between hierarchical and multilevel inheritance?
Hierarchical is one superclass with multiple direct subclasses; multilevel is a chain where each class extends the one before it.
Give an example of hybrid inheritance.
Any design mixing two shapes, for example a hierarchy where `Animal` has subclasses `Bird` and `Fish` (hierarchical), and `Bird` further has its own subclass `Penguin` (multilevel) layered on top.
Does C++ allow multiple inheritance, and what's the risk?
Yes, directly. The risk is the diamond problem when two base classes share a common ancestor, which is solved with virtual base classes.