Inheritence

Inheritance play a very important role in object oriented programming language. It is the mechanism in java by which one class is allow to inherit or access the features(data fields and properties) of another class.
The keyword ‘extends’ use in Java for inheritance features.

Let us see an example

Types of inheritance in Java

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

1. Single Inheritance
In single inheritance, a subclass inherit the features of one superclass.


Let’s see an example

2. Multilevel Inheritance

In Multilevel Inheritance, a child class inherits a base class and as well as the child class also act as the base class to other class.
Let’s see an example

3. Hierarchical Inheritance

In Hierarchical Inheritance, one class present as a superclass for more than one sub class.
Let’s see an example

4. Multiple Inheritance

Java does not support multiple inheritance. In multiple inheritance ,one class can have more than one superclass and inherit properties of all parent classes. We use interface in Java to achieve multiple inheritance.
Let’s see an example

5. Hybrid Inheritance

It is a combination of two or more of the above types of inheritance. As Java doesn’t support multiple inheritance so the hybrid inheritance is also not possible. We use interface in Java to achieve hybrid inheritance.

Let’s see an example