INHERITANCE

In Java, it is possible to inherit attributes and methods from one class to another. We group the “inheritance concept” into two categories:

  • subclass,(child) – the class that inherits from another class
  • superclass, (parent) – the class being inherited from

To inherit from a class, use the extends keyword.

Types of inheritance:

1.SINGLE 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.

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.

Importance of inheritance:

  • Inheritance minimizes the complexity of a code by minimizing duplicate code. If the same code has to be used by another class, it can simply be inherited from that class to its sub-class. Hence, the code is better organized.
  • The efficiency of execution of a code increases as the code is organized in a simpler form.
  • The concept of polymorphism can be used along with inheritance.

REFERENCES:

https://www.w3schools.com/java/java_inheritance.asp

Leave a comment

Design a site like this with WordPress.com
Get started