Implementing multiple inheritances using interfaces in Java

Ā 

Implementing multiple inheritances using interfaces in Java

Write a Java program to demonstrate the role of interfaces while implementing multiple inheritances in Java.

Video Tutorial:

Before we understand, how to use an interface to implement multiple inheritances, we will discuss the types of inheritance supported by Java.

There are four types of Inheritance:

1. Single Inheritance

2. Multilevel Inheritance

3. Hierarchical Inheritance

4. Multiple Inheritance

1. Single Inheritance

In single inheritance, there is one parent per derived class. This is the most common form of inheritance.

Example:

1. Single Inheritance

2. Multilevel Inheritance

When a derived class is derived from a base class which itself is a derived class then that type of inheritance is called multilevel inheritance.

Example:

Multilevel Inheritance

3. Hierarchical Inheritance

The process of deriving more than one subclass from a single superclass is called hierarchical inheritance.

Example:

Hierarchical Inheritance

4. Multiple Inheritance

The process of deriving a single subclass from more than one superclass is called Multiple Inheritance.

Example:

Multiple Inheritance

Java does not support the concept of Multiple Inheritance using a typical class hierarchy. However, it is possible to implement Multiple Inheritance using the interface.

Multiple inheritance using Interface

The Multiple Inheritance can be represented as follows:

Multiple inheritance using Interface

The simple Java Program to implement Multiple Inheritance is as follows

public interface Interface1 
{
	void method1();
}
public interface Interface2 
{
	void method2();
}
class Demo implements Interface1, Interface2
{
	public void method1()
	{
		System.out.println("The method from Interface 1");
	}
	public void method2()
	{
		System.out.println("The method from Interface 2");
	}
}
public class MultipleInheritanceDemo 
{
	public static void main(String args[]) 
	{
		Demo d = new Demo();
		d.method1();
		d.method2();
	}
}

Output:

The method from Interface 1

The method from Interface 2

Summary:

This tutorial discusses, Implementing multiple inheritances using interfaces in Java. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds