9/1/2021 Multiple inheritance by Interface in Java
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
Live Demo
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();
https://www.tutorialspoint.com/multiple-inheritance-by-interface-in-java 1/3
9/1/2021 Multiple inheritance by Interface in Java
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
Multiple Inheritance in C++
Multiple inheritance in JavaScript
C# and Multiple Inheritance
Why multiple inheritance is not supported in Java
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?
Difference Between Single and Multiple Inheritance
Inheritance in Java
What is diamond problem in case of multiple inheritance in java?
Does Java support multiple inheritance? Why? How can we resolve this?
https://www.tutorialspoint.com/multiple-inheritance-by-interface-in-java 2/3
9/1/2021 Multiple inheritance by Interface in Java
Multilevel inheritance in Java
https://www.tutorialspoint.com/multiple-inheritance-by-interface-in-java 3/3