Different Types of Inheritance in Java with Example Program

Different Types of Inheritance in Java with Example Program

In this tutorial of Java programming, we will see the different types of inheritance in Java. I will also be explaining each type with the Java program example. Pictorial representation of each type shown in this tutorial will make your job easy to understand.

What is inheritance?

In object-oriented programming, the mechanism of acquiring the properties and functionalities of one class into another class is known as inheritance. The main intent of inheritance lies in providing reusability of code so that common properties and functionalities can be extended from the other class.

In Java, inheritance showcases the IS-A relationship, i.e., a relationship between parent and child. So, let’s see the parent and child classes first.

Parent Class:

The class whose properties and functionalities are inherited by another class is called parent class. It is also called as a superclass or a base class.

Child Class:

The class which inherits the properties of the other class is called child class. It is also called as a subclass or a derived class.

Now, why do we need Inheritance?

The creation of a new class with the help of the properties of an already created class is the basic notion of inheritance, i.e., the data members (instance variables) and member functions (methods) of the base class can be used in derived class.

So there are many advantages of using inheritance in Java:

  • Reusability: Child class can use the data members and function defined in the parent class.
  • Using the same code for parent and child class, reduce the line of code.
  • Testing and debugging code will be easier.

The syntax for Inheritance:

The extends keyword is used to depict inheritance in Java. Here we are using Super as the parent class and Sub as the child class.

class Sub extends Super
{
        //code
}

Make a note that, the child class extends all the properties that are declared as public or protected. While the member declared as private can be accessed using public or protected getter and setter methods.

Different Types of Inheritance in Java:

Java defines varied types of inheritance namely–

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance

In object-oriented programming, there are also multiple inheritances and hybrid inheritance. Unlike other programming languages, Java does not support these multiple and hybrid inheritance. Why are they not supported? We will see that at the end of the post.

1) Single Inheritance:

When a single child class extends the properties of the parent class, then it is said to showcase single inheritance. Single inheritance can be represented as follows.

Single Inheritance Example

For example,

class Shape{
	void draw()
	{
		System.out.println(“Draw shape”);
	}
}
class Circle extends Shape
{
	void drwaCircle(){
		System.out.println(“Draw Circle”);
	}
 public static void main(String args[])
{
	Circle c  = new Circle();
	c.draw();
	c.drawCircle();
}
}

Output:

Draw shape
Draw Circle

2) Multilevel Inheritance:

When classes extend the properties of each other level by level is known as multilevel inheritance. For instance, class Z extends Y and class Y extends X. The pictorial representation below will help you understand the gist of multi-level inheritance better.

Multilevel Inheritance Example

Above is a pictorial representation of Multi-Level Inheritance.

A simple example is followed:

class Watch
{
	void display()
	{
		System.out.println(“WATCH”);
	}
class Titan extends Watch
{
	void property()
	{
		System.out.println(“TITAN”);		
	}
}
class Raga extends Titan
{
	void type()
	{
		System.out.println(“RAGA”);
		System.out.println(“Classic Collection”);
	}
}

class TestDemo
{
	public static void main(String args[])
	{
		Raga r = new Raga();
		r.display();
r.property();
r.type();
	}
}

Output:

WATCH
TITAN
RAGA
Classic Collection

3) Hierarchical Inheritance:

When one single class is inherited by multiple subclasses is known as hierarchical inheritance. Before we discuss an example of hierarchical inheritance, let us look at the pictorial representation of hierarchical inheritance first.

Hierarchical Inheritance Example

A simple example is discussed below:

class Laptop
{
	void display()
	{
		System.out.println(“Working...”);
	}
class Dell extends Laptop
{
	void print()
	{
		System.out.println(“Dell Inspiron”);
	}
}
class Lenovo extends Laptop
{
	void show()
	{
		System.out.println(“Lenovo YOGA”);
	}
}
class TestDemo
{
	public static void main(String args[]){
		Dell d = new Dell();
		d.print();
		d.display();
		Lenovo l = new Lenovo();
		l.show();
		l.display();
	}
}

Output:

Dell Inspiron
Working...
Lenovo YOGA
Working...

Why is multiple inheritance and hybrid inheritance not supported by classes in Java?

The basic gist for not supporting multiple inheritance is to avoid ambiguity caused by it. As hybrid inheritance is a mixture of the different types of inheritances that exist. Thus like multiple inheritance, hybrid inheritance also can’t be implemented.

This is all from the different types of inheritance in Java. If you have any doubt feel free to write a comment.



1 Comment

Leave a Reply

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