Inheritance in Java.

Inheritance is a process of writing a new class by using existing class functionality. ‘The process by which one class acquires the properties(instance variables) and functionalities of another class is called inheritance’. This happens when one class adopts the non-private properties and methods of one class. These classes will then be referred to as superclass and subclass.

  • The existing class is called super class or base class or parent class.
  • New class is called sub class or derived class or child class.
  • Use of inheritance is code re-usability.
  • Most importantly, we use inheritance if we want to increase features of class.

For ex.: Let's say that a student has attributes like age, location, and this class has existed for a long time. After some time, we need to add more attribute like pan card to student.

What options do we have?
Option 1 : We can modify class student
Option 2 : We need to extend student class and add an attribute in that new subclass

  • The first option is a violation of principles of java: 'Java classes are not open for modifications', so we should not modify an existing one. This will increase unit testing which will have to be done again for the existing class.

  • The second option is, without disturbing existing class, we add variable in another class by making subclass. So, the unit testing needs to be done for the child and not parent.

  • Super class members inherits to subclass provided they are eligible by access specifiers and if they are not already present in subclass.

  • We cannot assign super class to subclass.
  • We cannot extend final class.
  • We cannot extend class which has only private constructor, but if we have private constructor as well as public constructor then we can extend super to sub class. And in that case only public constructor will work.
  • Java doesn't support multiple inheritance in case of classes, but it supports it in case of interfaces.
  • Class cannot extend itself.
If we assign subclass reference to super class reference, it is called ‘dynamic dispatch’.

Consider the scenarios given below:

Only change this given class for all scenarios given below:

// Scenario 1

// Scenario 2

Now change the main method with different scenarios:

This example assigns sub class to super class.
This is allowed in java. i.e. A a = new B() in Scenario 3

// Scenario 3

Remember these Rules: In this case a is eligible to call

  • At compile time, everything of A class will be called other than that everything shows error at compile time. This means that in this case line no.(4) and line no.(7) will have problems.

  • At run time, all variables and methods of class A will get called, but all those methods which are overridden by class B will get executed. Or exactly similar methods of A into B will be called (Observe Output).

// Scenario 4

Try changing main method like below:

// Scenario 5

// Scenario 6

Following are the various types of inheritances:

  • Simple/Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

  • In this, there will be only one super class and one sub class
  • Every class has a super class as Object, and the package for Object class is java.lang.Object.
single inheritance in java

  • When a class extends a class, which in turn extends another class, is called multi-level inheritance
multilevel inheritance in java

  • One class has many super classes
  • This is not allowed in java
  • One needs to understand why this is not allowed, as you may be asked this question in an interview.
multiple inheritance in java

  • Note 1 : Multiple Inheritances are very rarely used in software projects. Using multiple inheritances often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class

  • Note 2 : Most of the new object oriented languages like Small Talk, Java, and C# do not support multiple inheritances. Multiple Inheritances is supported in C++

  • From the diagram above, if class A has m1 and class B also has m1 and they will have different implementations

  • As per inheritance concepts both methods should inherited to class C

  • If somebody creates object of class C, which m1 will get called? This is ambiguity of methods and therefore not allowed in java

  • Class C extends A, B like this syntax is not allowed in java

In such kind of inheritance one class is inherited by many sub classes. In the example below, class B, C and D inherits the same class. Class A is parent class (or base class) of B, C and D (or derived classes).

hierarchical inheritance in java

hybrid inheritance in java

  • Not allowed in Java
  • In simple terms, you can say that Hybrid inheritance is a combination of Single and Multiple inheritances. A typical flow diagram would look like above. A hybrid inheritance can not be achieved in the java in a same way as multiple inheritances can be!! Using interface it is allowed. Yes you heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java