Java Reference
In-Depth Information
@Override
public double getRate() {
return rate;
}
@Override
public void write() {
System.out.println(name + " is writing");
}
}
The following snippet of code shows how to use the Melodist class:
SingerWriter purcell = new Melodist("Henry Purcell");
purcell.setRate(700.00);
purcell.write();
purcell.sing();
Henry Purcell is writing
Henry Purcell is singing.
A person may sing as well as play games. Let's create a SingerPlayer interface to represent this kind of persons.
Let's inherit the interface from the Singer and Player interfaces as shown:
public interface SingerPlayer extends Singer, Player {
// No code for now
}
Trying to compile the SingerPlayer interface results in the following error:
SingerPlayer.java:4: error: interface SingerPlayer inherits abstract and default for getRate() from
types Player and Singer
The error resulted from the conflict in the two inherited versions of the getRate() method. The Singer
interface declares the getRate() method abstract and the Player interface declares it default. This causes a conflict.
The compiler cannot decide which method to inherit. This kind of conflict may arise when multiple versions of
the same default method are inherited from different superinterfaces. Consider the following declaration of the
CharitySingerPlayer interface:
public interface CharitySingerPlayer extends CharitySinger, Player {
}
Trying to compile the CharitySingerPlayer interface results in the following error:
CharitySingerPlayer.java:4: error: interface CharitySingerPlayer inherits unrelated defaults for
getRate() from types CharitySinger and Player
CharitySingerPlayer.java:4: error: interface CharitySingerPlayer inherits abstract and default for
setRate(double) from types CharitySinger and Player
 
Search WWH ::




Custom Search