Java Reference
In-Depth Information
The above class declaration will not compile. The class inherits the sing() , play() , and setRate() methods
without any conflicts. There are two choices to inherit the getRate() method:
Singer.getRate() method
The abstract
Player.getRate() method
None of the two versions of the getRate() method are more specific than others. In this case, the MultiTalented
class must override the getRate() method to resolve the conflict.
The following code for the MultiTalented class will compile. It overrides the getRate() method to resolve the
conflict. The method calls the default getRate() method of the Player interface and performs other logic. The class is
still declared abstract as it does not implemented abstract methods from the Singer and Player interfaces.
The default
public abstract class MultiTalented implements Singer, Player {
// A MultiTalented is paid the rate of a Player plus 200.00
@Override
public double getRate() {
// Get the default rate for a Player from the Player interafce
double playerRate = Player.super.getRate();
double rate = playerRate + 200.00;
return rate;
}
}
The instanceof Operator
You can use the instanceof operator to evaluate if a reference type variable refers to an object of a specific class or
its class implements a specific interface. It is a two-operand operator and it evaluates to a boolean value. The general
syntax of the instanceof operator is
<<referenceVariable>> instanceof <<referenceType>>
Let's consider the following snippet of code that defines interfaces Generous and Munificent , and classes Giver ,
GenerousGiver , MunificentGiver , and StingyGiver . Figure 17-2 shows a class diagram for these interfaces and classes.
Figure 17-2. A class diagram showing the relationship between interfaces and classes: Generous, Munificent, Object,
Giver, GenerousGiver, MunificentGiver, and StingyGiver
 
Search WWH ::




Custom Search