Java inheritance with Example

Facebooktwitterredditpinterestlinkedinmailby feather

Java inheritance

Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes a relationship between different classes.

It is an essential part of oops (object-oriented programming system).

As we know in real life, a son inherits the properties from his father just like that inheritance acts in Java. Java follows the similar concepts of son and father relationship.

Java inheritance represents a parent-child relationship which is also known as IS-A relationship.

Java has two classes

  • Parent class / Superclass / Base class
  • Child class/ Subclass /Derived class

A class whose properties are inherited is known as parent class whereas a class which inherits is known as Child class.

Java uses the ‘extends’ keyword to inherit the attributes and methods from a class.

Let’s have a look on the following example

Output

In the above example, the Child class CSTeacher holds the attributes and methods of parent class Teacher like designation, collegeName and void does().

The extends keyword indicates that you are using properties in a new class that derives from an existing class.

Advantage of Inheritance

  • Inheritance helps to achieve runtime polymorphism for method overloading.
  • Inheritance facilitates with code reusability.
  • Inheritance decreases the line of codes in Java program.
  • Inheritance minimizes the amount of duplicate code by sharing the standard code amongst several subclasses.
  • Inheritance facilitates with data hiding feature. The base class can decide to keep some data private so that the derived class can not alter it.

Disadvantage of Inheritance

The main disadvantage of inheritance is that two classes (base and derived) are tightly coupled that it cannot be used independently of each other.

Another disadvantage of the inheritance is that it increases time/ effort it takes the program through all levels of overloaded class.

Types of inheritance in Java

Different types of inheritance supported by Java are as follows.

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical inheritance

Single inheritance

In single inheritance, one class inherits only another class.

Single Inheritence in Java with Example

In the given figure, Class A is parent class, and Class B is Child Class, and class B is inheriting the properties and behavior. This process is known as single inheritance.

Example

Output

Multilevel inheritance

In multilevel inheritance, a child class will be inheriting a parent class as well as the child class, and also parent class for some other class. In the given diagram, class A serves as a parent class for the class B., and also class B serves as a parent class for class C.

This process is known as multilevel inheritance.

A class can’t access directly inherit the grandparent class.

Multiple Inheritence in Java

Output

Hierarchical inheritance

In hierarchical inheritance, a single base class serves many sub classes.

More than one child classes can inherit the properties of the parent class.

java hierarchical inheritance

In the given figure, Class B, Class C, and class D are the subclasses of Class A.

java hierarchical inheritance Example

Output

Super keyword in Java inheritance

In Java, the keyword ‘super’ and ‘this’ are very similar.

Super keyword refers to the object of the base (parent) class. It acts as a reference variable.

Super can be used in the following context.

  • Use of super with variables
  • Use of super with methods
  • Use of super with constructors

Some scenarios with the super keyword

Let’s see an example to understand the super keyword.  

Example 1

Output

In the given example, we have avoided the use of the super keyword.

Now see the example with super keyword.

Output

In the given example, when we use the super keyword then it refers the parent class object.

Example 2

Let’s understand how super keyword acts with parent class constructor.

Output

Question: Does Java supports multiple inheritance, explain?

Answer

Java doesn’t support multiple inheritance through classes, but It can be achieved through interfaces. It has some conflict like the diamond problem that is why java developers excluded this feature from java.

Multiple Inheritence

Some other languages like c++, common lisp support multiple inheritance, but java avoids it. It prevents multiple inheritance from avoiding ambiguity.

Question: Explain diamond problem in Java.

Answer:

The diamond problem

The following diagram and program will clear the image of the diamond problem.

diamond problem

Output-

As we can see in the above program on calling method m1() using D class object will cause complication. The program got confused whether it calls A’s method m1() or B’s method m1() because class D inheriting both class A and B. This complicated situation is called diamond problem.

Facebooktwitterredditpinterestlinkedinmailby feather