• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2021
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools

Java Program for Multiple Inheritance using Interface Example Explained

Pranati Paidipati/28152/2
CodeJAVA
YouTube CSEstack

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?
  • Java Program for Interface Inheritance
  • Java program for multiple inheritance using interface
  • Nested Interface Example

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.


« Abstraction in Java Explained

JavaJava Oops Concepts
Pranati Paidipati
I am a graduate in computer science with a creative bent of mind for writing content. With great gusto, I enjoy learning new things. I trail in database management system, and object-oriented programming languages like Java, C/C++.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Vane William
    December 19, 2019 at 9:17 pm

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

  • Reply
    Akhil
    December 29, 2019 at 9:11 pm

    Really worthy content… And good explanation.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Exception Handling
  11. Java- ‘throw’ vs ‘throws’ Keyword
  12. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- HashSet
  5. Java- HashMap vs HashSet

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2021 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard