Java Inheritance:

 

  • Inheritance is a feature of Object-Oriented Programming (OOP).
  • Inheritance allows a class to use the properties and methods of other classes.
  • In other words, the derived class receives states and behaviors from the base class.
  • The derived class is called subclass and base class is known as superclass.
  • Derivative classes can add additional variables and methods of their own.
  • These additional variables and methods differentiate the class derived from the base class.
  • In Java Inheritance is a compile-time system.
  • There can be a subclass in a super-class but only one superclass in a subclass can be due to the fact that Java does not support multiple Inheritance.

 

Why Use Inheritance in Java

 

  • Code reusability : Inheritance supports the concept of “reusability”, that is, when we want to create a new class and already there is a class in which some codes are included, we get our new class from the current class Can. By doing this, we are reusing the existing fields and methods.

 

Types of inheritance in Java

 

  • Single inheritance: refers to the relationship between a child and a parent class where one class extends the second class.
  • Multilevel inheritance: refers to the relationship between a child and a parent class where a class extends the child’s class.
  • Hierarchical inheritance: refers to the relationship of a child and parent class where more than one class is extends over the same class.
  • Multiple inheritance: refers to the concept of a class extending more than one class, which means that there are two basic classes of the child class.

 

Syntax:

Example for Single Inheritance :

Output: