Inheritance In Java…

Java is an approximately 100% object-oriented programming language. Everything which we deal with in java is an object or part of an object(except primitive data types).Java OOPs concept provides features like Inheritance, Encapsulation, Abstraction, Polymorphism. Here I will discuss Inheritance In Java which is one of the most important features of the Java OPPs concept.

Below are the topics, I will be discussing in this article:

  1. What is Inheritance?
  2. Why do we need Inheritance?
  3. Types of Inheritance in Java.

What is Inheritance?

Inheritance is a mechanism in which one class acquires all the properties and behaviors of a parent class i.e in inheritance we use the variables and methods of the previous class to the new class by adding some more variables and functions.


Syntax of Inheritance

extends is a keyword that is used to inherit the features of the Superclass.

Subclass:- Subclass is the class that inherits the other class, also known as child class or derived class.

Superclass:-It is the class whose features are inherited by the subclass, also known as parent class or base class.

Why do we need Inheritance?

It is often when we write our code there are more than one classes which are having the same variables and methods. To write the same code in every class is time-consuming. Hence we need to find a way so that we do not need to write the same code again and again in each class and the solution is Inheritance.

Inheritance allows us to write reusable codes and save our time to write the same code again and again. The advantage of inheritance is that the code which is already written in the parent class need not be written in the child class.

Types of Inheritance in Java.

Java supports three types of Inheritance.

a.Single Inheritance

b.Multilevel Inheritance.

c.Hierarchical Inheritance.

a.Single Inheritance

When a subclass inherits the features of one superclass, it is known as single inheritance.

Block diagram of Single Inheritance

In the above block diagram, Class A is the base class, and Class B is the derived class which is inherited from Class A. For more clarity let’s see an example through code.


In the above example, we can see that the SuperCar class is inheriting the features of the Car class so SuperCar is the derived class and Car is the base class here.

b.Multilevel Inheritance.

When a derived class inherits a base class and that base class also inherits another class, it is known as multilevel inheritance.

Block diagram of Multilevel Inheritance

In the above block diagram, Class C is the subclass of class B and Class B is the subclass of class A. Let’s see an example through code.


In the given example SuperCar class is inheriting the Car class and the Car class is inheriting the Vehicle class. So we can access the features of the Car class and Vehicle class through the object of the SuperCar class. Here Car class in the base class for SuperCar class and derived class for the Vehicle class.

c.Hierarchical Inheritance.

When two or more classes(derived classes) inherit a single base class, it is known as hierarchical inheritance.

Block diagram of Hierarchical Inheritance

In the given example the SuperCar and MiniCar Classes are inheriting the features of the same base class(Car class).

Points to remember:-

Whenever we use inheritance in our program, there should be always a proper and well-defined relationship between the child and parent classes. Suppose we create a class of Vehicle which inherits the features of an Animal class, when we run our program it will work fine but if we observe carefully, there is no any common features between the Animals and Vehicles in real-world so it’s doesn’t make any sense to use the features of Animal class in Vehicle class.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Expense Tracker

Vanilla JS — Rails API Flatiron Project

An In-Depth Introduction To Start Coding in PHP Today

Mexico on the digital golden rush: machine learning

Microservices Are Not a Silver Bullet. Learn Why.

How To Go From SQL Zero to SQL Hero

SQL Hero

What We Expect From Software Developers On Each Level

Moving PiP-player on Mac OS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ajit Kumar Singh

Ajit Kumar Singh

More from Medium

10 Beautiful and Low-maintenance Fish for Beginners (With Pictures)

CS373 Spring 2022: William Eng: Final Entry

Prim’s Algorithm

Career Options In Embedded System