Concept of Inheritance in Java

Inheritance in Java allows a subclass to inherit properties and methods of parent class. There are three types of inheritance in Java- Simple Inheritance, Multiple Inheritance and Hierarchical Inheritance.

Concept of Inheritance in Java

Concept of Inheritance in Java is considered one of the important features of Object Oriented Programming. Inheritance as the name suggest is used to derive classes from other classes.

The derived class is known as subclass or extended class and the class from which it is derived is known as parent class or base class. "extend" keyword is used to derive a sub class from parent class.

Let's understand the concept of inheritance in Java:

Inheritance is used to make things more specific. Sub class or extended class has the features similar to parent class and hence they extend it.

To know the concept of Inheritance clearly you must have the idea of class and its features like methods, data members, access controls, constructors, keywords this, super etc.

As the name suggests, inheritance means to take something that is already made. It is one of the most important feature of It is the concept that is used for reusability purpose.

There are three types of inheritance in java:

Simple Inheritance

In simple inheritance, a parent class is extended by one sub class, hence it is also known as single inheritance or one level inheritance.

Multilevel Inheritance

In Multiple Inheritance, a parent class is extended by sub class, which is further extended by another subclass. When a subclass is extended by another subclass, the former becomes the parent class for later. Multilevel inheritance can go up to any number of levels.

Java does not support Multiple Inheritance however it can be achieved by using more than one interfaces in a class.

super keyword is used to access the members of the parent class.

By using super keyword one can access the data variables of the parent class hidden by the sub class.

super keyword is also used to call parent class constructor in the subclass.

Hierarchical inheritance

In Hierarchical Inheritance, parent class is extended by more than one sub class in a tree structure.

Points to remember while using Inheritance concept in Java:

  • members that are declared private in parent class cannot be accessed directly by subclass
  • A subclass can inherit default members of parent class when in same package but cannot inherit them in other packages
  • subclass do not inherit constructors and initializer blocks
  • subclass can extend only one parent class

Benefit of using inheritance in Java:

Inheritance is used in java for the reusability of code.

Inheritance in Java allows property of the parent class to be inherited automatically by the sub class.

Inheritance allows access of properties and methods of parent class by a sub class.