What Is an Interface in Java ?

An interface in Java 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.

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 — playable, readable, drivable, and stoppable, for example. This type of adjective is commonly used for interface names.

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 implementation for every method declared by the interface.

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 HeartsGame implements Playable, CardGame
{
    // must implement methods of the Playable
    // and CardGame interfaces
}

Here, the HeartsGame class implements two interfaces: Playable and CardGame.

An interface is a kind of type, just like a class. As a result, you can use an interface as the type for a variable, parameter, or method return value.

Consider this snippet of code:

Playable game = getGame();
game.play();

Here, you assume that the getGame method returns an object that implements the Playable interface. This object is assigned to a variable of type Playable in the first statement. Then the second statement calls the object’s play method.

Alternatively, you could call the constructor of a class that implements the Playable interface. For example, suppose that a class named HeartsGame implements the Playable interface. Then, you could use the following code:

Playable game = new HeartsGame();
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com