Types of Inheritance
Explore the different types of inheritance in Java including single-level, multilevel, and hierarchical inheritance. Understand how classes inherit properties in various ways and why Java disallows multiple inheritance. This lesson helps you grasp core inheritance concepts critical for coding and designing object-oriented programs.
We'll cover the following...
The following are the types of inheritance in Java:
- Single-level inheritance
- Multilevel inheritance
- Hierarchical inheritance
Let’s understand what each type means.
Single-level inheritance
In single-level inheritance, a class inherits properties from a single class. For example, class B inherits class A.
Multilevel inheritance
In multilevel inheritance, a class inherits properties from a class, which, again, has inherited properties. For example, class C inherits class B, and class B inherits class A.
Hierarchical inheritance
In hierarchical inheritance, multiple classes inherit properties from a single class. For example, Class B inherits Class A, and Class C inherits Class A.
🚨 An important alert
Java does not support multiple inheritance, as we discussed in detail before.
This means that a class cannot extend more than one class. Therefore, the following is illegal:
public class C extends B, A