16

I have two Java interfaces and one implementing class.

(I have used Eclipse to run the program directly, and I did not try to check any compiler warning et cetera by explicitly compiling from the command line.)

Why do they run without problem? Why does Java allow this, even when it satisfies the "contract" of both interfaces but create ambiguity in implementing class?

Updated the example.

public interface CassettePlayer {
    void play();
}

public interface DVDPlayer {
    void play();
}

public class CarPlayer implements CassettePlayer,DVDPlayer{

    @Override
    public void play() {
        System.out.println("This plays DVD, screw you Cassette !");
    }

    public static void main(String args[]) {
        CarPlayer cp = new CarPlayer();
        cp.play();

        CassettePlayer firstInterface = new CarPlayer();
        firstInterface.play();

        DVDPlayer secondInterface = new CarPlayer();
        secondInterface.play();
    }
}
3
  • 2
    Why should they have a problem? That's the question ;)
    – Eng.Fouad
    Mar 25, 2012 at 20:46
  • 1
    If you wished you could have an abstract class with a sayHello method and make Sample extend the abstract class. There would also be no problem.
    – emory
    Mar 25, 2012 at 20:51
  • 2
    hey it is instantiating the interface :O May 14, 2013 at 6:27

7 Answers 7

37

This scenario specifically allowed in the Java Language Specification, section 8.1.5:

It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:

interface Fish { int getNumberOfScales(); }
interface Piano { int getNumberOfScales(); }
class Tuna implements Fish, Piano {
   // You can tune a piano, but can you tuna fish?
   int getNumberOfScales() { return 91; }
}

the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano; it is considered to implement both.

The text then goes on to note that if the method signatures had different return types, such as double and int, there would be no way to implement both interfaces in the same class and a compile time error would be produced.

0
5

For this issue it's necessary to understand what interfaces are for.

An interface is a kind of "contract" so that one knows which methods are compulsorily implemented in a Class with that interface.

So if you need a Class implementing "DVDPlayer" (because you need the method "play()"), you'll find CarPlayer. Same goes for the need of a Class implementing CassettePlayer. That's the technical explanation.

But of course in your semantic coding you should ensure that CarPlayer's method "play()" satisfies the semantics of both DVDPlayer and CassettePlayer. I think in a practical application it will be a bad practice.

Of course in your example it's a bad idea to have two interfaces declaring the same method. More practically, you should have made an interface "Player" with method "play()" and have two other, more specific interfaces DVDPlayer and CassettePlayer (with specific methods for DVDs and cassettes) who inherit from Player. On the onther hand, if you don't need specific methods for DVDs or cassettes, then you don't need two different interfaces only implementing one and the same method - just use one interface Player, that'll be enough.

2

In this situation, there is no issue because both interfaces have same method signature. But What about this ?

interface Animal {
    public void eat() throws IOException;
}

interface Plants {
    public void eat() throws NullPointerException;
}

Which one is choosen by the compiler ? Why does it get error below code ?

public class Test implements Animal, Plants {

    public void eat() throws IOException {

    }
}

Compiler says : Exception IOException is not compatible with throws clause in Plants.eat()

1

There is no conflict because they both specify the same contract, implementing classes only provide the one method which is called when referenced via either interface.

1

Why not? The class is satisfying the contracts defined by both interfaces.

1

The class implements both interfaces - so no issue. Of course, this sort of thing should be avoided in more complex scenarios where unintended behaviour might result.

1

The following page contains an example of a class that implements two interfaces that have the

1) same variable name 2) same method in each interface.

http://www.j2eeonline.com/java-tm-fundamentals-II/module2/interface-ambiguous-fields.jsp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.