Inheritance in Java

Mukta Sharma
4 min readMay 27, 2021

--

Image Source: Google

Hi there, are you new to Java? If yes,you may get some benefit out of this article as this is based upon my own experience while working in a real time project. I will try to explain the Inheritance concept in best way possible so that you can relate it with your project ( if you are working on one). If not,you do not need to worry as this article will focus on the meaning and conceptualization of Inheritance in Java.

We should understand the OOP (Object Oriented Programming) concept in Java primarily. Inheritance is one of them. So, what do you mean by Inheritance? Let's get started.

In general terms, when a child inherits the properties from his parents, the process is call Inheritance. I hope, you will agree with me, right ?

Similarly, in Java world, when a child class inherits the properties of its parent class, the process is termed as “Inheritance”. Child class is also known as subclass or derived class. Parent class is also called as super class or Base class.

Example: Visual representation of parent class and child class

Source: google
Image Source: Google

Inheritance represents IS-A relationship which is also known by Parent-child relationship.

Syntax:

class Super {//methods and fields}class Sub extends Super {//methods and fields}

Now, the question comes to your mind how do we inherit properties of a class. The answer would be by using “extends” keyword as shown above.

With the help of “extends” keyword, you can inherit the properties (characteristics) of a class to another class.

Syntax:

  1. class Subclass-name extends Superclass-name
  2. {
  3. //methods and fields
  4. }

Example:

class Car extends Vehicle{//methods and fields}Source:googleSimilarly, another example could be:class JellyFish extends Fish
{
//methods and fields
}

Here, Car is a subclass which inherits the properties from its Superclass which is Vehicle in this example. Or we can say Car Is-A vehicle. In another example, JellyFish is a subclass which extends Fish base class. Let’s move on to understand the different types of Inheritance in Java.

Types of Inheritance in Java

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

Lets learn about all the types one by one.

Single Inheritance :

When one subclass inherits the properties of one super class. There could be many examples to relate to single inheritance. One of the easy example is given below:

Example:

Image Source: Google

In the above diagram, Animal is the Base class or Super class which has 2 methods named eat () and sleep(). We derived one sub class called Dog which has one method say bark(). Here, Dog IS-A animal relationship. So, this is called Single Inheritance.

Multilevel Inheritance:

In this inheritance, multiple classes are involved. class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. You can understand it in a more efficient way by looking at the below image.

Example:

Image Source: Google

Hierarchical Inheritance:

In Hierarchical inheritance, one class serves as a super class for more than one subclass. In the below image, A is served as a Base class/super class for B,C,D which are the derived classes or sub classes.

Example:

Image source: google

Multiple Inheritance:

Please note that Java does not support multiple inheritance with classes but this can be possibly achieved by using “Interfaces” in Java. Multiple Inheritance occurs when a class inherits from more than one base class.

Example:

Image source: google

Here, A&B are two base classes and Class C is the derived class which is derived from class A& class B.

How do we achieve Multiple Inheritance in Java?

An example to understand this is by the below representation:

Image Source: google

In the above diagram, multiple inheritance is achieved by using Interfaces in Java.

Benefits of Inheritance:

  1. Code Reusability-Methods (or the fields) written in base class need not be repeated in child class.
  2. This makes the code reusable,easily readable and less time consuming.

I hope, this will help you in understanding about Inheritance and different types of Inheritance in Java. You can start writing with some basic code to implement inheritance for practice purposes.

Please feel free to share your inputs. Lets learn,grow and improve together. Thank you for stopping by!

--

--

Mukta Sharma

Hi,I write on software testing( Manual testing,Automation,API,Mobile,InterviewQ&A etc.). My YT link :https://www.youtube.com/channel/UCppaYb_VotTvHxb6b3pE_5Q