Inheritance in Java

priya karthik
3 min readAug 21, 2019

Inheritance in Java:

Java inheritance is a mechanism in which an object acquires all the properties and behaviours of a parent object. It’s an important part of object-oriented programming (OOP) The idea behind inheritance in Java is that you can create new classes based on existing classes.

By inheriting from an existing class, you can reuse methods and fields from the main class. In addition, you can also add new methods and fields to your current class.

Inheritance represents the IS-A relationship, which is also known as the parent-child relationship.

Important terminology:

• Superclass: The class whose characteristics are inherited is known as a superclass (either a base class or a main class).

• Subclass: A class that inherits another class is known as a subclass (or a derived class, extended or secondary class). . The subclass can add its own fields and methods in addition to the superclass fields and methods.

Reusability: Inheritance supports the concept of “reuse”, ie when we want to create a new class and there is already a class that includes part of the class. code we want we can derive our new class from the existing class. In doing so, we are reusing the fields and methods of the existing class.

Types of Inheritance in Java:

· Single Inheritance

· Multiple Inheritance

· Multilevel Inheritance

· Hybrid Inheritance

Single Inheritance:

Unique inheritance is very easy to understand. When one class extends just another class, we call it a single inheritance. The flowchart below shows that class B extends only one class that is A. Here A is a parent class of B and B would be a child class of A.

Multiple Inheritance:

Multiple Inheritance refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.

Multilevel Inheritance:

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A.

Hybrid Inheritance:

In this type of inheritance, a class is inherited by many subclasses. In the following example, classes B, C, and D inherit the same class A. A is the parent class (or base class) of B, C, and D.

Hierarchical Inheritance:

In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example class A will be inherited by class B, class C and class D. Class A will be acting as a parent class for class B, class C and class D.

Hope you have got a better understanding towards the different types of inheritance in Java.
For More Info click here: Java training in chennai

--

--