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

  • One Super class and one sub class
  • Sub class inherits the features of super class

java single inheritance
Fig:java single inheritance

Write a program for single level inheritance.

Multi level Inheritance

In multi level inheritance inheritance exists in multiple level.

  • One Super Class
  • It has a child class
  • Child class has a child class

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

Multilevel Inheritance

program for Multi level inheritance.

Result

Hierarchical Inheritance

java hierarchical inheritance
java hierarchical inheritance

Hybrid  Inheritance

java Hybrid  Inheritance
java Hybrid Inheritance

Result

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

Write a program for Hierarchical Inheritance.

Result