Inheritance In Java

Inheritance is a process where one class inherits the properties of another class.

Inheritance In Java

Let’s say we have two classes namely Parent Class and Child Class. Child class is also known as Derived Class. As per the above definition, the Child class inherits the properties of the Parent Class. The main purpose of Inheritance is to obtain Code Reusability. We can achieve run time polymorphism with inheritance. We will see what is run time polymorphism in few minutes.

Learn what is a Class and Object by using this link

Assume we have a Class named Laptop, Apple MacBook Pro, Lenovo Yoga. Apple MacBook Pro and Lenovo Yoga classes extend the Laptop Class to inherit the properties of the Laptop Class.

We will see detailed explanation with some example programs about Inheritance in the post related to Inheritance.

In inheritance, we use two keywords namely extends and implements

Extends:

We use extends keyword in Java to allow the child class to inherit all the properties (data members and methods) of the parent class and in addition to these, we can also create new data members and methods. If the properties are private then the child class cannot inherit those properties from the parent class.

Technically, we say that child class has IS-A relationship with the parent class

As per the IS-A relationship, we can say QA IS-A Employee

Employee Class:

QA Class:

In the above example, QA object can access the properties of its own as well as Employee class.

Implements:

We use implements keyword in Java to inherit the properties from an interface. Interfaces cannot be extended by the classes.

We will see this in Interface section.

Types of Inheritance:

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance

There are two more inheritances such as Multiple and Hybrid Inheritance which are supported through interface only. We will see those in the Interface section.

Single Inheritance:

Single Inheritance in Java refers to a child and parent class relationship. In this one class extends another class say Class B extends Class A

Single Inheritance

 

 

Output:

Multilevel Inheritance:

Multilevel Inheritance in Java refers to a child and parent class relationship. In this a class extends a child class say Class C extends Class B and Class B extends Class A

Multilevel Inheritance

 

 

 

 

Output:

Hierarchical Inheritance:

Hierarchical Inheritance in Java refers to a child and parent class relationship. In this more than one class extends the same class say Class B, Class C extends Class A

Hierarchical Inheritance

 

 

 

 

Output:

Multiple Inheritance:

Multiple Inheritance is not possible in Java. In this a child extends two parents classes say Class C extends Class A and Class B.

Multiple Inheritance

Hybrid Inheritance:

Combination of more than one type of inheritance.

Hybrid Inheritance

Must Read: Java Tutorial

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

Leave a Comment

Share via
Copy link
Powered by Social Snap