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();
}
}
This scenario specifically allowed in the Java Language Specification, section 8.1.5:
The text then goes on to note that if the method signatures had different return types, such as
doubleandint, there would be no way to implement both interfaces in the same class and a compile time error would be produced.