Inheritance in Java

In this post, we will see about Inheritance in java. It is one of the OOPs principles apart from Abstraction, encapsulation and polymorphism.

Introduction

The word Inheritance is quite familiar with everyone. In common terms, the word means the bequeathing of property and characteristics from generation to generation. For example, the property or characteristics of parents are handed down to their children and the forthcoming generations.

Object Oriented Programming (commonly OOP) concepts are based on real life examples, where every entity in existence can be represented as an object. Thus, being one of the fundamental concepts in OOP, Inheritance is based on the example we discussed earlier – bequeathing of properties and characteristics from parents to their children.
Inheritance in Java, as derived from the concept of OOP, is defined as the process by which a child class or object (known as subclass) inherits the behaviors and properties(methods and variables) from its predecessors or parent class(known as super class). Let us delve a little deeper into the concepts of Inheritance in java with the following sections.

Basic Syntax

Inheritance in Java is implemented by the use of the keyword extends. This special word makes the Java compiler understand that the current class is inheriting or extending another class. Let us look at the following snippet example to understand the basic syntax.

The above snippet shows the use of the extends keyword. In the language of Java, the use of ‘extends’ indicates that the class B is a child or a subclass of the class A, which is known as the super class or parent. Inheritance represents an IS-A relationship between classes. Here, B is a child or subclass of A.

Example of Inheritance in Java

Inheritance

The picture given alongside displays a simple representation of inheritance in Java.
Here, the class Parent contains an integer variable a and is a super-class to class Child which contains an integer variable b Let us see the representation of this picture by means of a code example.

The above code snippet on execution, provides the following result:

Value of A =5
Value of B =7
Sum=12

From the above sample we see that the child class B is able to access the variable a of class Parent and use it in its own class. Thus we see that using inheritance, sub-classes may utilize variables and methods from their super-classes.

Types of Inheritance in Java

There are various forms of representing a Parent-Child relationship in Object Oriented Programming. Java supports three types of Inheritance on the basis of classes. The various types of inheritance shall be discussed further in this section and how they are realized using Java.

Single Inheritance

This is the simplest form of inheritance in Java and is a simple ONE to ONE relationship between two classes. A basic example of single inheritance has already been discussed in the section above, where a single Child class had inherited the attributes of its Parent class.

Single Inheritance

The picture given alongside represents a single inheritance between classes A(super-class) and B(sub-class)
Let us see a small code snippet as an example of Single Inheritance.
Code:

The above code snippet executes to produce the following output:

You are viewing a method of A

Multi-Level Inheritance

Multi-Level Inheritance as its name suggests looks more like a tier-based structure. The picture given alongside represents a multi-level inheritance structure. In Multi-Level Inheritance, each class extends only a single class in the form of a multi-level or multi-tiered architecture.

For instance, from the figure alongside, we see that class C is a sub-class of class B and class B is a sub-class of class A. Thus, B will inherit the attributes and behavior of class A and C, in turn, will inherit from both.
Let us see a code snippet as an example of Multi-Level Inheritance.

On executing the above code, the following output is obtained:

Colts are baby horses
Horses neigh
Animals eat

Hierarchical Inheritance

Hierarchical Inheritance is a type of inheritance where many sub-classes extend a single super-class.

Hierarchical Inheritance

The picture given alongside represents a basic form of Hierarchical Inheritance. This form of inheritance is basically multiple single inheritances where all sub classes inherit from a single super class. Here, classes B and C are sub-classes of class A and inherit its attributes and behavior.
Let us see a code snippet as an example of Hierarchical Inheritance.
Code:

The above code, on execution, produces the following output:

Wolves howl
Animals eat
Lions roar
Animals eat

Multiple and Hybrid Inheritance

Multiple Inheritance is a type of Inheritance in Object Oriented Programming where a single sub-class extends multiple super-classes. Hybrid Inheritance is a mixed form of inheritance comprising of Multiple and Multi-Level Inheritance. Multiple Inheritance is not supported by Java.

Multilevel inheitance

Above picture shows Multiple Inheritance

Hybrid inheritance

This picture depicts Hybrid Inheritance.

Why does Java not support Multiple Inheritance?

To reduce complexity and ambiguity, Java does not allow Multiple Inheritance using classes. Let us explain the ambiguity with an example.
Say, there is a class A having a method display() and class B having another method display(). A third class C, extends both A and B and as per the concept of inheritance, is able to access both the display() methods of A and B. Now, when creating an object of C and calling the display() method, the compiler will not be able to discern which display method to call as both classes A and B have a method having the same name and are extended by class C.

Code:

In order to prevent this ambiguity, Java does not allow multiple inheritance between classes and throws a compile-time error when multiple inheritance is attempted.
Note: Java, however, simulates Multiple Inheritance by using Interfaces.

Conclusion

Inheritance is a strong weapon of Java that helps to make it a widely acceptable language. It helps to reduce code duplication and also cuts down on the bugs. With the code written in the parent class, you no longer need to write the same code for multiple child classes that has the same properties. In this way, inheritance in java implements code reusability to ensure better accessibility to users.

That’s all about inheritance in java.

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, when a function is called by the Java compiler the task of correctly locating the method for the respective function is binding. Depending on when the compiler is […]

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.