Java Reference
In-Depth Information
LISTING 7.10
continued
System.out.println ("No, the answer is " + q2.getAnswer());
}
}
OUTPUT
What is the capital of Jamaica? (Level: 4)
Kingston
Correct
Which is worse, ignorance or apathy? (Level: 10)
apathy
No, the answer is I don't know and I don't care
a class implements multiple interfaces, they are listed in the implements clause,
separated by commas. For example:
class ManyThings implements Interface1, Interface2, Interface3
{
// contains all methods of all interfaces
}
In addition to, or instead of, abstract methods, an interface can also contain
constants, defined using the final modifier. When a class implements an inter-
face, it gains access to all the constants defined in it.
The interface construct formally defines the ways in which we can interact
with a class. It also serves as a basis for a powerful programming technique called
polymorphism, which we discuss in Chapter 10.
The Comparable Interface
The Java standard class library contains interfaces as well as classes. The
Comparable interface, for example, is defined in the java.lang package. The
Comparable interface contains only one method, compareTo , which takes an object
as a parameter and returns an integer.
The intention of this interface is to provide a common mechanism for compar-
ing one object to another. One object calls the method and passes another as a
parameter as follows:
if (obj1.compareTo(obj2) < 0)
System.out.println ("obj1 is less than obj2");
 
Search WWH ::




Custom Search