Inheritance in Java-Types and Examples

Inheritance in Java is the process of acquiring the properties of one class object into another class object.

It allows the hierarchical classification. Inheritance also provides the facility of reusability.

In Java programming, a class that is inherited is called a superclass or sub class or parent class and class that does the inheriting is called sub class or child class.     

If we want to inherit a class, we simply incorporate the definition of one class into another class by using extends keyword.

General form of a class declaration that inherits a super class

Type of Inheritance in Java

There are following type of inheritance

  1. Single level Inheritance
  2. Multi-level Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance

Note: Java does not support multiple inheritance.        

Single level Inheritance

Property of single level inheritance is as below

Single level Inheritance in Java
Fig:java single inheritance
  • One Super class and one sub class
  • Sub class inherits the features of super class

Write a program for single level inheritance.

Output

Multi level Inheritance

In multi level inheritance inheritance exists in multiple level.

Multi level Inheritance  in Java
Multilevel Inheritance
  • One Parent Class
  • It has a child class
  • Child has again child class

This process may continue and each child class inherits feature of all parent class.

program for Multi level inheritance in Java

Result

Hierarchical Inheritance

One Parent class can have multiple child class this type of inheritance is known as Hierarchical Inheritance.

  • One Parent class
  • Multiple Child class
  • Each Child Inherits the feature of Parent
hierarchical inheritance in Java
java hierarchical inheritance

Write a program for Hierarchical Inheritance.

Result

Hybrid  Inheritance

Combination of two or more Inheritance type is known as Hybrid Inheritance

Hybrid  Inheritance in Java
java Hybrid Inheritance

Note: A subclass can access the public member of super class. A subclass can’t access the private member of super class.

Super Class Cant Access Sub Class Member

A sub class can access all the public member of super class but reverse is not true. Super class cannot access sub class data.

Result

A sub class can’t access the private member of super class.   

Result