Inheritance in Java


Inheritance is an important feature of Java programming language. Inheritance uses a concept Generalization which means extraction of class from generic class. The generic class is the main class or the parent class of the program. Inheritance means to make objects out of other objects. Consider the following diagram to understand the concept Inheritance:

inheritance-in-java

The class person is the generic class. In inheritance the objects of the child class have all the properties of the objects of the parent class. The code can be reused if it has used the concept of inheritance. The concept of inheritance is used in programs to achieve the run time polymorphism.

Inheritance in java programming language is basically sued to create new classes that are based on the classes that are defined already in the same code. When a class is inherited from another class or when a child class is created from the parent class then we can use the methods of the parent class, we can also add new methods and fields. In Java or any other programming language inheritance represents the IS-A relationship that is the parent child relationship.

Why use inheritance in Java?

The following are the main purposes to use inheritance in Java programming language:

  1. To override methods
  2. To reuse the code

The methods are overridden by using inheritance in Java programming language so that we can achieve the run time polymorphism.

Syntax of java inheritance:

The following is the syntax to implement the concept of inheritance in Java programming language:

class <name of sub class> extends <name of super class>

{

methods and fields of the class

}

In the syntax above, the sub class is the new class and the super class will be the class that will be inherited. The extend keyword in the above syntax is used to indicate that a new class is going to be derived from the existing class.

Understanding the simple example of inheritance:

Consider the following diagram:

understanding-the-simple-example-of-inheritance

In the above figure, the person class is the super class and the teacher and student classes are inherited or derived from the person class hence they are the sub classes. The relationship between the two classes that is a super class and a sub class is IS-A. If we have a super class as person and a sub class as student then the relationship between these two classes will be:

Student IS-A Person which means that the class student is a type of the class person.

Now consider the following example in which we have implemented inheritance and the super class is the person class and sub class is student class:

CODE:

class Person {

string name = “Stuart”;

}

class Student extends Person {

int roll = 121;

public static void main (string args []) {

Student s = new Student ();

system. out. println (“Name of programmer is” +s. name);

system. out. println (“Roll number of student is” +s. roll)’

}

}

OUTPUT:

Name of programmer is Stuart

Roll number of student is 121

In the above example, two classes are created, ne is the super class and the other is the sub class. The super class is the Person and the sub class is the student class. The super class that is the Person class has a data member that is ‘name’. And the sub class that is Student has one data member that is roll. In the main function or method, the object of the sub class that is student class is created. And through this object we accessed the data members of both the super and the sub classes. It is quite clear from the above example that the data members or fields or methods of the super class can be accessed by the sub class by using objects. And the code is reusable.

Types of inheritance in Java:

In Java programming language, there are three types of inheritance:

  1. Single
  2. Hierarchical
  3. multilevel

Hierarchical Inheritance:

In hierarchical inheritance there are more than one parent classes. This concept is illustrated as follows:

hierarchical-inheritance

Here A is the parent class and B and C are the child classes.

Multilevel Inheritance:

In multilevel inheritance there is a mid class who is a child and a parent class at the same time that is:

multilevel-inheritance

A is the parent of B and B is the parent of C. In other words B is the child of A. C is only child. C is directly linked to B and indirectly to A.

Child classes are derived classes and parent classes are base classes.

Single Inheritance:

In single inheritance there is only one super class and one sub class that is derived from the super class. Consider the following figure:

single-inheritance

In java programming language the multiple ‘inheritance’ is not supported through class.

In Java programming language the multiple and hybrid inheritance is supported through the interface only.

Multiple ‘Inheritance’:

Consider the following figures in which the concepts of multiple and hybrid inheritance is cleared:

multiple-inheritance

The above figure is showing multiple class inheritance. We have two parent classes that is A and B and C is the child class of both the classes. It should be noted here that the classes A and B are not linked to each other.

Hybrid inheritance:

Consider the following diagram for the hybrid inheritance:

hybrid-inheritance

The class A is the parent of all the classes. That is B and C are the child classes of the class A and b is the child class of both classes B and C. In this way the class A will be the parent class of all classes and we can access its methods, fields and data members.

Q. Why multiple ‘inheritance’ is not supported in Java?

In Java programming language, the multiple ‘inheritance’ is not supported. This is because to maintain the simplicity of the programming language. When multiple ‘inheritance’ is implemented in any program then the program becomes more complex.

For example, in a java program the user has declared three classes that is A, B and C. Suppose that the classes A and B are the parent classes and the class C is the child class therefore, C will inherit A and B. If the classes A and B have same name of methods or the same methods and the user wants to call a method from the child class that is by using the object of the child class, then there will be an ambiguity. That is how the compiler will know that which method is to be called either the method of class A is to be called or the method of class B is to be called.

The java programming language renders a compile time error when two classes are inherited in a program. It does not matter if the classes have same methods or different methods, the java will render a compile time error when two classes are inherited.

Consider the following example in which we have inherited two classes and got a compile time error by java:

CODE:

class A {

void display () {

system. out. println (“Hello world”);

}

class B {

void display () {

system. out. println (“Hello Java”);

}

class C extends A, B {

public static void main (string args []) {

C c1 = new C ();

c1. display ();

}

}

OUTPUT:

Compile time error

In the above example, two classes are declared A and B. The class C inherits the two classes. Both A and B have same methods. But the java compiler will create a compile time error.