2017-06-05

java Interface

Interface
An interface is similar to an abstract class. However, it can only include
abstract methods and final fields (constants), and it can’t be used as a base
class. Instead, a class implements an interface by providing an implementation
for each method declared by the interface.
Here’s a basic interface that defines a single method, named Playable, that
includes a single method named play:
public interface Playable
{
void play();
}
This interface declares that any class that implements the Playable interface
must provide an implementation for a method named play that accepts
no parameters and doesn’t return a value.

This interface has a few interesting details:
✦ The interface itself is declared as public so that it can be used by other
classes. Like a public class, a public interface must be declared in a
file with the same name. Thus, this interface must be in a file named
Playable.java.

✦ The name of the interface (Playable) is an adjective. Most interfaces
are named using adjectives rather than nouns because they describe
some additional capability or quality of the classes that implement the
interface. Thus, classes that implement the Playable interface represent
objects that can be played.
In case you haven’t been to English class in a while, an adjective is a
word that modifies a noun. You can convert many verbs to adjectives
by adding –able to the end of the word. For example: playable, readable,
drivable, and stoppable. This type of adjective is commonly used for
interface names.
✦ Another common way to name interfaces is to combine an adjective with
a noun to indicate that the interface adds some capability to a particular
type of object. For example, you can call an interface that provides methods
unique to card games CardGame. This interface might have methods
such as deal, shuffle, and getHand.
✦ All the methods in an interface are assumed to be public and abstract.
If you want, you can code the public and abstract keywords on
interface methods. However, that’s considered bad form, because it
might indicate that you think the default is private and not abstract.
Implementing an interface
To implement an interface, a class must do two things:
✦ It must specify an implements clause on its class declaration.
✦ It must provide an implementation for every method declared by the
interface.
For example, here’s a class that implements the Playable interface:
public class TicTacToe implements Playable
{
// additional fields and methods go here
public void play()
{
// code that plays the game goes here
}
// additional fields and methods go here
}
Here, the declaration for the TicTacToe class specifies implements
Playable. Then, the body of the class includes an implementation of the
play method.
A class can implement more than one interface:
public class Hearts implements Playable, CardGame
{
// must implement methods of the Playable
// and CardGame interfaces
}
Here, the Hearts class implements two interfaces: Playable and CardGame.
A class can possibly inherit a superclass and implement one or more interfaces.
For example:
public class Poker extends Game
implements Playable, CardGame
{
// inherits all members of the Game class
// must implement methods of the Playable
// and CardGame interfaces
}

No comments:

Post a Comment