Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (2.6k points)

Java doesn't allow multiple inheritance, but it allows implementing multiple interfaces. Why?

1 Answer

0 votes
by (7.2k points)

The reason behind this is to prevent ambiguity in JAVA. Consider a case where class B extends class A and Class C and both class A and C have the same method display(). Now java compiler cannot decide, which display method it should inherit. To prevent such situations, multiple inheritances is not allowed in java.

 

 Implementing multiple interfaces in java

interface Flyable {

void fly();

}

 

interface Eatable {

void eat();

}

 

// Bird class will implement both interfaces

class Bird implements Flyable, Eatable {

 

public void fly() {

System.out.println("Bird flying");

}

 

public void eat() {

System.out.println("Bird eats");

}

}

 

/*

 * Test multiple interfaces example

 */

public class Sample {

 

public static void main(String[] args) {

 

Bird b = new Bird();

b.eat();

b.fly();

}

 

}

Related questions

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...