What is Inheritance in Java

What you will learn about inheritance in java here:

  • What is inheritance in java
  • Why we use inheritance in java
  • Important points about inheritance in java
  • Structure of inheritance in java
  • Types of inheritance in java
  • Single level inheritance in java
  • Multi level inheritance in java
  • Hierarchical inheritance in java
  • Multiple level inheritance in java

What is inheritance in java?

Inheritance in java is a process of acquiring the properties i.e states and behavior of one class in another class.  In simple words, we can say one class accessing the states and behavior of another class is nothing but the inheritance. In Java, we achieve inheritance using the keyword extends. extends is represented by the Solid arrow.

Why we use inheritance in java?

1)When multiple classes have the same properties i.e states and behavior, then writing the same properties again and again in each class increases the code redundancy. Code redundancy means writing the unnecessary code again and again.
Why we use inheritance in java
Code redundancy is avoided by using inheritance in java which is shown below. Common properties are written in one class which is called a parent class or base class. classes that are using the common properties are called a child class or derived class. In the below example, the Human class is parent class and Doctor, Engineer, Teacher, Worker are child classes.
Inheritance in java

2)We can achieve code reusability by using inheritance in java.

3)We can generalization by using inheritance in java.

4)We can achieve polymorphism and abstraction by using inheritance in java.

Important points about inheritance in java

Below important points about inheritance in java are given.
1)We can only inherit variables and methods of class which is nothing but the states and behavior of class respectively.

2)Inheritance is implemented by using a keyword called extends.

3)We can’t inherit Constructor of class.

4)we can’t inherit private methods and variables of a class.

5)When multiple classes implementing the same or common properties i.e variables and methods then, we write those same or common properties in one class to avoid the code redundancy.

6)The class which has common or same properties is called a super class or parent class or base class.

7) The class which acquiring or extending the properties from the parent class is called a sub class or childclass or derived class.

8)Only one class can extends another one class at a time.

9)When the compilation of code happens, every class in java automatically extends super most class called Object class.
java inheritance
10)If a class is extending another class then it doesn’t extend object class directly but still, it indirectly extends Object class through multi-level inheritance.
multi-level inheritance example

Structure of inheritance

The general structure of inheritance in java is shown below.
Structure of inheritance

Types of Inheritance

Below types of inheritance in java are listed and explained with the help of example.
1)Single level inheritance
2)Multi level inheritance
3)Hierarchical inheritance
4)Multiple level inheritance
5)Hybrid inheritance

Types of Inheritance

Single Level Inheritance:

One class acquiring or extending another class is nothing but the Single level inheritance. In the below example, the Human class is a parent class or base class which has common properties and the Engineer class is a child class which is acquiring the properties of the Human class (parent class).  Engineer class acquiring the properties from parent class using keyword called extends followed by parent class name which is shown below.
single level inheritance in java

Multi Level Inheritance:

One class extending another class and again another class extending the subclass is called as multi level inheritance.
multilevel inheritance in java

Hierarchical Inheritance:

When more than one classes acquire the properties of one class, then such type of inheritance is called a Hierarchical Inheritance.
hierarchical inheritance in java

Multiple Level Inheritance:

When one class acquires the properties of more than one class then such type of inheritance is called as multiple level inheritance. But this is not possible to implement using a class as one class can acquire the properties of one class at a time. Therefore Multiple Level Inheritance is implemented by using Interface. Please click on the below link to know, how implement multiple level inheritance in java using Interface.

Multiple inheritance java

Hybrid Inheritance:

Hybrid inheritance is a combination of Hierarchical Inheritance and Multiple Level Inheritance. It is not possible to implement using class. It is possible to implement using Interface.

You may also like...

1 Response

  1. Johnd830 says:

    An attention grabbing dialogue is worth comment. I believe that you must write extra on this subject, it won’t be a taboo topic but typically persons are not enough to speak on such topics. To the next. Cheers

Leave a Reply