The Techie King

Inheritance in Java

Article By The Techie King

Inheritance is an important pillar of OOP.It is the mechanism in java by which one class is allowed to inherit the features fields and methods of another class.Inheritance represents the IS-A relationship which is also known as a parent-child relationship.The class which inherits the properties of other is known as subclass and the class whose properties are inherited is known as superclass.during inheritance only the object of the subclass is created, not the superclass

extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

class A extends B {}

Why use inheritance in java

  • For Method Overriding.
  • For Code Reusability.

Types of inheritance in java

  • Single Inheritance : When a class inherits another class, it is known as a single inheritance. In the example given below, Son class inherits the Father class, so there is the single inheritance.

  • Multilevel Inheritance : When there is a chain of inheritance, it is known as multilevel inheritance. example : Son class inherits the Father class which is inherits the Grandfather class, so there is a multilevel inheritance.

  • Hierarchical Inheritance : When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Son and Daughter classes inherits the Father class, so there is hierarchical inheritance.

  • Multiple inheritances : In java, we can achieve multiple inheritances only through Interfaces. In the image below, Class C is derived from interface A and B.Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes

  • Hybrid Inheritance : Hybrid Inheritance is mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

Scroll to Top