Interface
An interface is similar to a class, but the body of an interface can include only abstract methods and final fields (constants). A class implements an interface by providing code 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.
Notice that the name of the interface (Playable) is an adjective. Most interfaces are named with 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.
All the methods in an interface are assumed public and abstract. To implement an interface, a class must do two things:
It must specify an implements clause on its class declaration.
It must provide an ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access