

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiple inheritance by Interface in Java
An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.
A program that demonstrates multiple inheritance by interface in Java is given as follows:
Example
interface AnimalEat { void eat(); } interface AnimalTravel { void travel(); } class Animal implements AnimalEat, AnimalTravel { public void eat() { System.out.println("Animal is eating"); } public void travel() { System.out.println("Animal is travelling"); } } public class Demo { public static void main(String args[]) { Animal a = new Animal(); a.eat(); a.travel(); } }
Output
Animal is eating Animal is travelling
Now let us understand the above program.
The interface AnimalEat and AnimalTravel have one abstract method each i.e. eat() and travel(). The class Animal implements the interfaces AnimalEat and AnimalTravel. A code snippet which demonstrates this is as follows:
interface AnimalEat { void eat(); } interface AnimalTravel { void travel(); } class Animal implements AnimalEat, AnimalTravel { public void eat() { System.out.println("Animal is eating"); } public void travel() { System.out.println("Animal is travelling"); } }
In the method main() in class Demo, an object a of class Animal is created. Then the methods eat() and travel() are called. A code snippet which demonstrates this is as follows:
public class Demo { public static void main(String args[]) { Animal a = new Animal(); a.eat(); a.travel(); } }
- Related Questions & Answers
- Why multiple inheritance is not supported by Java?
- Java and multiple inheritance
- Java Program to Implement Multiple Inheritance
- Multiple Inheritance in C++
- Multiple inheritance in JavaScript
- Why multiple inheritance is not supported in Java
- C# and Multiple Inheritance
- How multiple inheritance is implemented using interfaces in Java?
- Does Python support multiple inheritance?
- Can an interface extend multiple interfaces in Java?
- Can an interface in Java extend multiple interfaces?
- Inheritance in Java
- What is diamond problem in case of multiple inheritance in java?
- Difference Between Single and Multiple Inheritance
- Does Java support multiple inheritance? Why? How can we resolve this?