Understating of Inheritance in Java

By | September 22, 2018

This tutorial talks about Understating of  Inheritance in Java.This is one of the important Oops feature in object oriented programming language.

Inheritance is one of the important fundamental Oops concepts . The other three are-

  1. Encapsulation
  2. Polymorphism
  3. Abstraction

Inheritance is a process by which one class acquires, all the properties and behaviors of another class. The class whose members are inherited is called the Super class (or base class), and the class that inherits those members is called the Sub class (or derived class).

The relationship between the Super and inherited subclasses is known as IS-A relationship.

As Example – If Person is a super class and there are subclasses Employee and Manager derived from the Person super class then Employee IS-A Person and Manager IS-A Person.

Types of Inheritance

In java, OOPS concepts there are 5 types of Inheritance –

Single Inheritance

In this type of inheritance, a sub class is derived from a single Super class.

Multi-level inheritance

In this type of inheritance, a subclass is created from another sub class thus having levels of hierarchy.

Multiple Inheritance

In this type of inheritance, a sub class can have more than one super class. Note – Java does not support multiple inheritance.

Hierarchical Inheritance

In this type of inheritance more than one sub classes are created from the same super class.

Hybrid inheritance

This inheritance is the combination of more than one inheritance types so it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multiple inheritance.

What is inherited?

In Java when a class is extended, sub-class inherits all the public, protected and default (Only if the sub-class is located in the same package as the super class) methods and fields of the super class.

What is not inherited?

Private fields and methods of the super class are not inherited by the sub-class and can’t be accessed directly by the subclass.

Constructors of the super-class are not inherited. We have a concept of constructor chaining in Java which determines in what order constructors are called in case of inheritance.

Example code showing inheritance using extends keyword


 It can be seen how methods and fields of super classA are visible in classB, private field b is not visible.

Multiple Inheritance in Java

Java does not support multiple inheritance which means a class can not extend more than one super classes at a time. Though, a class can implement more than one interface.

Some of key points about Inheritance –

  • In Java inheritance can be achieved using extends, in case of class and using implements in case of interface.
  • Constructors of the super class are not inherited by the sub class.
  • Private fields and methods of the superclass are not inherited by the subclass and can’t be directly accessed.
  • Members with default access in the super class are inherited by the subclass only if they both reside in the same package.
  • We can use final keyword in Java to restrict that no class can extend a given class.
  • If both super class and sub class have a method with same signature then subclass method is said to be overriding the super class method.
  • Java does not allow Multiple inheritance; a class can extend at most one super class.

Would you like to watch on YouTube:

That’s all about Understating of Inheritance in Java.

You May Also Like:

Encapsulation in Java
Abstraction in Java
Method Overloading or Static Polymorphism in Java
Method Overriding or Dynamic Polymorphism in Java
Difference Between Encapsulation and Abstraction in Java
Association Aggregation And Composition in Java

If you have any feedback or suggestion please feel free to drop in the blow comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *