Java - Types of Inheritance-interview
Q1. What are the different types of inheritance in Java?
Java supports:
- Single Inheritance – one class inherits another.
- Multilevel Inheritance – a class inherits from a derived class (grandparent → parent → child).
- Hierarchical Inheritance – multiple classes inherit from the same parent.
Java does not support multiple inheritance with classes (to avoid ambiguity) but allows it with interfaces.
Q2. Why does Java not support multiple inheritance with classes?
To avoid the Diamond Problem, where a class can inherit conflicting methods from two parents. Java solves this by allowing multiple inheritance using interfaces
Q3. What is the diamond problem in inheritance?
The diamond problem occurs in multiple inheritance when a class inherits the same method from two parents, creating ambiguity. Java avoids this by not supporting multiple inheritance with classes
Q4. What is hybrid inheritance in Java? Is it supported?
Hybrid inheritance is a mix of more than one type (e.g., single + multiple).Java does not support hybrid inheritance with classes, but it can be achieved through interfaces. Hybrid inheritance is a mix of more than one type (e.g., single + multiple).
Q5. Can a class extend multiple classes in Java? If not, why?
No. A class can only extend one class. This ensures simplicity, avoids conflicts, and keeps Java’s design clean. Multiple inheritance is supported only with interfaces.
Q6. Give a real-world analogy for types of inheritance.
- Single: A Son inherits traits from his Father.
- Multilevel: Grandson inherits from Father, who inherits from Grandfather.
- Hierarchical: Multiple siblings inherit traits from the same Father.
- Multiple (via interfaces): A Smartphone is both a Camera and a Phone.