Java Program for Multiple Inheritance using Interface Example Explained

Java Program for Multiple Inheritance using Interface Example Explained

In the previous tutorial, we have learned about how abstraction is achieved using abstract classes. Now let us learn about interfaces in Java.

Along with that, this tutorial will cover inheritance using interface.

Table of Contents:

What is an Interface?

An interface is similar to a class but not exactly class.

So the question is –what is the difference between class and interface in java?

The interface consists of data members and member functions but these member functions are abstract (i.e. they don’t have any implementation) by default.

In other words, the interface is just another way to accomplish abstraction in Java.

The syntax of interface declaration is as follows:

interface 
{
	//data members
	//abstract member functions
}

As we know, a class can inherit another class, in the same way, an interface can extend another interface. But when a class wants to use interface, it implements it.

Example of an interface:

interface Readable
{	
	void read();
}
class Student implements Readable
{
    public void read()
	{
		System.out.println(“Student Reads.”);
	}
}
class Test
{
	public static void main(String args[])
	{
		Readable r = new Student();
		r.read();
	}
}

Output:

Student Reads.

Interface Inheritance Java Program

We all know about different types of inheritance in Java, as we have seen in our earlier tutorial. Now let’s dive into interface inheritance.

When one interface inherits or extends the properties of another interface is known as interface inheritance.
A simple illustration of the interface inheritance concept is as follows:

Example of Interface Inheritance:

interface Story 
{
	void tell();
}
interface Readable implements Story
{	
	void read();
	
}
class Student implements Readable
{
    public void read()
    {
		System.out.print(“Student reads ”);
    }
    public void tell()
    {
	System.out.print(“ amazing Stories..”);
    }
}
class Test
{
	public static void main(String args[])
	{
		Readable r = new Student();
		r.read();
		r.tell();
	}
}

Output:

Student reads amazing Stories..

Java Program for Multiple Inheritance using Interface

Multiple inheritances in Java is depicted in two ways-

  • When a class implements multiple interfaces
  • When interface extends multiple interfaces

Example of Multiple Inheritance using Interfaces:

A basic example of multiple inheritances in Java using interfaces is as follows:

interface Writeable
{
	void writes();
}
interface Readable 
{	
	void reads();
	
}
class Student implements Readable,Writable
{
    public void reads()
    {
	stem.out.print(“Student reads.. ”);
    }
    public void writes()
    {
	System.out.print(“ Student writes..”);
    }

    public static void main(String args[])
    {
	Student s = new Student();
	s.reads();
	s.writes();
    }
}

Output:

Student reads..
Student writes..

Nested Interface:

One interface can hold another interface inside it. This is known as a nested interface.

Example of Nested Interface:

interface Task
{
    void display();
    interface Play
    {
        void show();
    }
}

Note that, In Java 8, we are provided with the provision to create static and default methods in an interface.

Summing up Java program for multiple inheritance using interface, interfaces also become the medium to achieve abstraction. They are similar to a class but by default contain abstract member functions.



3 Comments

  1. Hi to every single one. I’m new to programming. I find really consistent and precious Information on this website. Thank you!

Leave a Reply

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