Java Reference
In-Depth Information
LISTING 7.9
continued
//-----------------------------------------------------------------
// Returns the answer to this question.
//-----------------------------------------------------------------
public String getAnswer()
{
return answer;
}
//-----------------------------------------------------------------
// Returns true if the candidate answer matches the answer.
//-----------------------------------------------------------------
public boolean answerCorrect (String candidateAnswer)
{
return answer.equals(candidateAnswer);
}
//-----------------------------------------------------------------
// Returns this question (and its answer) as a string.
//-----------------------------------------------------------------
public String toString()
{
return question + "\n" + answer;
}
}
but it does not restrict it from having additional ones. It is common for a class
that implements an interface to have other methods.
Listing 7.10 shows a program called MiniQuiz , which uses some Question objects.
An interface and its relationship to a class that implements it can be shown in
a UML class diagram. An interface is represented similarly to a class node except
that the designation <<interface>> is inserted above the interface name. A dot-
ted arrow with a closed arrowhead is drawn from the class to the interface that
it implements. Figure 7.3 shows a UML class diagram for the MiniQuiz program.
Multiple classes can implement the same interface, providing alternative defini-
tions for the methods. For example, we could implement a class called Task that
also implements the Complexity interface. In it we could choose to manage the
complexity of a task in a different way (though it would still have to implement
all the methods of the interface).
A class can implement more than one interface. In these cases, the class must
provide an implementation for all methods in all interfaces listed. To show that
Search WWH ::




Custom Search