Inheritance Architectures: 5 Types, Visual Diagrams & The Diamond Problem
Single, Multiple, Multilevel, Hierarchical, Hybrid inheritance and solving the deadly diamond.
Mock tests for SSC, Banking, Railways, UPSC & state exams
Real exam pattern, negative marking and a worked solution for every question.
Inheritance allows a subclass (child class) to inherit features from a superclass (parent class). In this article, we map the 5 core types of inheritance with visual topology diagrams matching page 4 of the official curriculum, and dissect the infamous Diamond Problem.
1. What is Superclass vs Subclass?#
• Superclass (Parent Class): An entity which allows other classes to inherit from itself. It serves as the base template.
• Subclass (Child Class): An entity which inherits state and behavior from another class. It can introduce specialized methods or override existing ones.
2. The 5 Types of Inheritance (With Visual Diagrams)#
The 5 classic inheritance structures depicted in the official curriculum:
- 1. Single Inheritance: A derived class inherits from exactly one base class (A -> B).
- 2. Multiple Inheritance: A derived class inherits directly from two or more base classes (A, B -> C). Supported in C++, disallowed for classes in Java to prevent ambiguity.
- 3. Multilevel Inheritance: A class inherits from a derived class, forming a chain (A -> B -> C).
- 4. Hierarchical Inheritance: Multiple derived classes inherit from a single common base class (A -> B, C, D).
- 5. Hybrid Inheritance: A composition of two or more inheritance types (e.g., Hierarchical + Multiple forming a diamond).
The 5 Inheritance Architectures (Visual Topology)
Exact structural blueprints as defined in standard SDE placement examinations.
Direct linear lineage. Exactly one derived class inherits from one base class.
One derived class inherits features directly from two or more parent classes.
Chained transitive inheritance. Class C inherits from B, which inherits from A.
One parent class serves as the common base for multiple independent child classes.
Combines hierarchical and multiple inheritance topologies, creating complex polymorphic structures.
Practise placement aptitude on the real test pattern
TCS NQT, Infosys, Wipro, Accenture and Cognizant style mocks with section-wise analysis.
3. The Infamous Diamond Problem in Multiple Inheritance#
Question 17 from the Tricky Questions list: "What is the diamond problem in inheritance?"
The Diamond Problem occurs in Multiple Inheritance when a class (D) inherits from two intermediate classes (B and C) that both share a common ancestor class (A).
If class A has a method `display()`, and both B and C override `display()`, when D calls `display()`, the compiler cannot determine whether to call B’s version or C’s version! This introduces ambiguity in method resolution.
- How C++ Solves It: C++ provides "Virtual Base Classes" (`class B : virtual public A`). This guarantees that only one single instance of class A is shared in the inheritance hierarchy of D.
- How Java Avoids It: Java completely forbids multiple inheritance of classes (a class can only extend ONE class). Java allows multiple inheritance of interfaces, where ambiguities in default methods must be explicitly resolved using `InterfaceName.super.method()`.
The Diamond Problem in Multiple Inheritance
`class B : virtual public A` ensures only one shared instance of A is created, dissolving the duplicate copy.
Java forbids multiple class inheritance entirely, avoiding diamond state conflicts while allowing interface contracts.
| 1 | // C++: Resolving the Diamond Problem with Virtual Inheritance |
| 2 | #include <iostream> |
| 3 | |
| 4 | class A { |
| 5 | public: |
| 6 | void print() { std::cout << "Base A\n"; } |
| 7 | }; |
| 8 | |
| 9 | // Virtual inheritance ensures only 1 copy of A exists in D |
| 10 | class B : virtual public A {}; |
| 11 | class C : virtual public A {}; |
| 12 | |
| 13 | class D : public B, public C {}; |
| 14 | |
| 15 | int main() { |
| 16 | D obj; |
| 17 | obj.print(); // Ambiguity resolved! Calls single shared A::print() |
| 18 | return 0; |
| 19 | } |
Be prepared to explain why Java decided against multiple class inheritance. James Gosling and the Java architects cited the Diamond Problem and pointer memory layout complexity as the main reasons!
Test Your Concept Mastery
Verify your retention on: Inheritance Architectures: 5 Types, Visual Diagrams & The Diamond Problem
How does C++ solve the ambiguity of the Diamond Problem?
Ready for a full proctored assessment?
Take a free quick test on the real exam pattern, with a worked solution for every question.