Inheritance in java

Uday Patil
3 min readOct 21, 2023

--

Java, Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well.

Why Do We Need Java Inheritance?

  • Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can directly use the parent class code.
  • Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by which Java achieves Run Time Polymorphism.
  • Abstraction: The concept of abstract where we do not have to provide all details is achieved through inheritance. Abstraction only shows the functionality to the user.

Important Terminologies Used in Java Inheritance

  • Class: Class is a set of objects which shares common characteristics/ behavior and common properties/ attributes. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
  • Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a base class or a parent class).
  • Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

1.Single level inheritance

As the name suggests, this type of inheritance occurs for only a single class. Only one class is derived from the parent class. In this type of inheritance, the properties are derived from a single parent class and not more than that. As the properties are derived from only a single base class the reusability of a code is facilitated along with the addition of new features. \

2. Multi-level Inheritance

The multi-level inheritance includes the involvement of at least two or more than two classes. One class inherits the features from a parent class and the newly created sub-class becomes the base class for another new class.

As the name suggests, in the multi-level inheritance the involvement of multiple base classes is there. In the multilevel inheritance in java, the inherited features are also from the multiple base classes as the newly derived class from the parent class becomes the base class for another newly derived class.

3. Hierarchical Inheritance

The type of inheritance where many subclasses inherit from one single class is known as Hierarchical Inheritance.

Hierarchical Inheritance a combination of more than one type of inheritance.

It is different from the multilevel inheritance, as the multiple classes are being derived from one superclass. These newly derived classes inherit the features, methods, etc, from this one superclass. This process facilitates the reusability of a code and dynamic polymorphism (method overriding).

4. Multiple Inheritance

Multiple inheritances is a type of inheritance where a subclass can inherit features from more than one parent class. Multiple inheritances should not be confused with multi-level inheritance, in multiple inheritances the newly derived class can have more than one superclass. And this newly derived class can inherit the features from these superclasses it has inherited from, so there are no restrictions. In java, multiple inheritances can be achieved through interfaces.

5. Hybrid Inheritance

Hybrid inheritance is a combination of more than two types of inheritances single and multiple. It can be achieved through interfaces only as multiple inheritance is not supported by Java. It is basically the combination of simple, multiple, hierarchical inheritances.

Guide : http://www.vevcodelab.com/

--

--