Solving Multiple Inheritance

Java allows only one parent class, so interfaces let a class take on more than one set of abilities without the problems of multiple inheritance.

Journeyman · Java · OOP · Updated

By

An interface is like a 100% abstract class. So, if interfaces are just special classes, why do we need them?

Many OOP languages that do not support interfaces allow child classes to inherit from multiple parent classes.

Multiple inheritance

This can lead to unexpected coding challenges that you now have to deal with. For instance, if you create a riding mower and then turn it on, which parent turnOn() method should be called, or should they both be called?

RidingMower lawnRider = new RidingMower("Meadow Pro","5500",500);
lawnRider.turnOn();

Because this challenge of dealing with multiple inheritance often resulted in buggy code, a pattern that emerged in OOP was that developers would create one primary parent class from which a child could inherit variables and methods. Then, if a class needed to inherit from another class, additional parents would be designed as fully abstract classes. This way, potential method naming collisions could be avoided.

Java took this approach one step further. In Java, and in several other modern OOP languages, it is illegal to extend more than one parent class.

Java only allows single inheritance

Adding Interfaces

Java only allows you to inherit from a single class, but, you can implement as many interfaces as you need. By introducing the interface construct, Java ensured that the additional "inherited classes" would be 100% abstract.

RidingMower implements multiple interfaces

Do it: one turnOn for two interfaces

The riding mower is set up the Java way: one parent class, Machine, which gives it its make, and two interfaces after it, Drivable and BladeController. Both interfaces ask for a turnOn() method. The starter doesn't compile yet: Java says RidingMower doesn't write the interfaces' methods. Your job is to write them.

The two interfaces are given at the bottom of the file (the next lesson shows how to write your own):

  • Drivable asks for String turnOn() and String steer(String direction).
  • BladeController asks for String turnOn().

A class keeps an interface's contract by writing each of its methods as an ordinary public method, with the same name, parameters and return type.

For example, a Meadow Pro mower:

RidingMower mower = new RidingMower("Meadow Pro");
Call Returns
mower.turnOn() "Meadow Pro is running"
mower.steer("left") "Turning left"

Your job: write the methods the two interfaces ask for in RidingMower.

  • Write one public String turnOn(). It returns the mower's make, then " is running". That one method keeps both interfaces' contracts.
  • Write public String steer(String direction). It returns "Turning ", then the direction.
  • Leave Machine, the two interfaces and RidingMower's first line as they are.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets