Thursday, 9 April 2015

Inheritance in Java

Inheritance is one of the four fundamental OOP concepts. The other three being -

Inheritance Concept

Inheritance is a mechanism, 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 Shape is a super class and then there are subclasses Circle and Rectangle derived from the Shape super class then Circle IS-A Shape and Rectangle IS-A shape.

Types of Inheritance

Going by 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.
Single Inheritance

Multi-level inheritance

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

Multiple Inheritance

In this type of inheritance, a sub class is created using more than one super class. Note Java does not support multiple inheritance.
Multiple Inheritance

Hierarchical Inheritance

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

Hybrid inheritance

This type of inheritance is the combination of more than one inheritance type as described above. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multiple inheritance.
Hybrid inheritance

Inheritance in Java

Inheritance in Java is done using

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. There is 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

//Super class
class A{
 public int a = 10;
 private int b = 15;
 public void displayFromA(){
  System.out.println("Value of a " + a);

// sub class 
class B extends A{
 int c = 20;
 public void displayFromB(){
  System.out.println("Value of field a " + a);
  // This line will give compiler error as b is not visible
  // System.out.println("Value of field b " + b);
  // Calling inherited method directly as if 
  // it is a method of this class, as it is inherited 
  // from super class
  // ok, field of this class
  System.out.println("Value of field c " + c);

public class InheritanceDemo {
 public static void main(String[] args) {
  B b = new B();
  // calling the method of class B using class B object
  // calling the method of class A using class B object



Value of field a 10
Value of a 10
Value of field c 20
Value of a 10

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

Multiple Inheritance in Java

Java doesn’t support multiple inheritance which means a class can extend at most one super class only. Though, a class can implement more than one interface.

Points to note -

  • In Java inheritance is done 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.
  • Using final keyword in Java, a class can restrict that no class can extend it.
  • 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.
  • Multiple inheritance is not allowed in Java, a class can extend at most one super class.

That's all for this topic inheritance in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Method overriding in Java
  2. interface in Java
  3. abstract class in Java
  4. Association, Aggregation and Composition in Java
  5. Java OOP interview questions

You may also like -

>>>Go to Java Basics page

1 comment: