Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Java Interfaces simulate multiple inheritance?

I am reading "The Java Tutorial" (for the 2nd time). I just got through the section on Interfaces (again), but still do not understand how Java Interfaces simulate multiple inheritance. Is there a clearer explanation than what is in the book?

like image 381
jacknad Avatar asked Aug 24 '10 13:08

jacknad


People also ask

How does interface provide multiple inheritance 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.

Do Java interfaces support multiple inheritance?

Java allows multiple inheritance using interfaces. Interfaces could only define abstract methods, that is, methods without any implementation. So if a class implemented multiple interfaces with the same method signature, it was not a problem. The implementing class eventually had just one method to implement.

Can an interface have multiple inheritance?

Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators.

How interfaces solve the problem of multiple inheritance?

Interfaces contain only public constant definitions and method headers. Interfaces can only extend other interfaces, not classes. Interfaces can extend multiple interfaces, and classes can implement multiple interfaces.


1 Answers

Suppose you have 2 kinds of things in your domain : Trucks and Kitchens

Trucks have a driveTo() method and Kitchens a cook() method.

Now suppose Pauli decides to sell pizzas from the back of a delivery truck. He wants a thing where he can driveTo() and cook() with.

In C++ he would use multiple inheritance to do this.

In Java that was considered to be too dangerous so you can inherit from a main class, but you can "inherit" behaviors from interfaces, which are for all intents and purposes abstract classes with no fields or method implementations.

So in Java we tend to implement multiple inheritance using delegations :

Pauli subclasses a truck and adds a kitchen to the truck in a member variable called kitchen. He implements the Kitchen interface by calling kitchen.cook().

class PizzaTruck extends Truck implements Kitchen {    Kitchen kitchen;     public void cook(Food foodItem) {       kitchen.cook(foodItem);    } } 

He is a happy man because he can now do things like ;

pizzaTruck.driveTo(beach); pizzaTruck.cook(pizzaWithExtraAnchovies); 

Ok, this silly story was to make the point that it is no simulation of multiple inheritance, it is real multiple inheritance with the proviso that you can only inherit the contract, only inherit from empty abstract base classes which are called interfaces.

(update: with the coming of default methods interfaces now can also provide some behavior to be inherited)

like image 60
Peter Tillemans Avatar answered Sep 20 '22 21:09

Peter Tillemans