Tino APCS

Interfaces

In Lesson A14, Boolean Algebra, it was learned that Java has a class-like form called an interface that can be used to encapsulate only abstract methods and constants. An interface can be thought of as a blueprint or design specification. A class that uses this interface is a class that implements the interface.

An interface is similar to an abstract class: it lists a few methods, giving their names, return types, and argument lists, but does not give any code. The difference is that an abstract class my have its constructors and some of its methods implemented, while an interface does not give any code for its methods, leaving their implementation to a class that implements the interface.

interface and implements are Java reserved words. Here is an example of a simple Java interface:

public interface Comparable {
  public int compareTo(Object other);
}


This looks much like a class definition, except that the implementation of the compareTo() method is omitted. A class that implements the Comparable interface must provide an implementation for this method. The class can also include other methods and variables. For example,

class Location implements Comparable {
  public int compareTo(Object other) {
    ... // do something -- presumably, compare objects
  }
    ... // other methods and variables
}


Any class that implements the Comparable interface defines a compareTo() instance method. Any object created from such a class includes a compareTo() method. We say that an object implements an interface if it belongs to a class that implements the interface. For example, any object of type Location implements the Comparable interface. Note that it is not enough for the object to include a compareTo() method. The class that it belongs to must say that it “implements” Comparable.

A class can implement any number of interfaces. In fact, a class can both extend another class and implement one or more interfaces. So, we can have things like:

class Fish extends SeaCreature implements Locatable, Eatable {
  ...
}


An interface is very much like an abstract class. It is a class that can never be used for constructing objects, but can be used as a basis for making subclasses. Even though you can't construct an object from an interface, you can declare a variable whose type is given by the interface. For example, if Locatable is an interface defined as follows

public interface Locatable {
  Location location();
}


then if Fish and Mermaid are classes that implement Locatable, you could say

/**
 * Declare a variable of type Locatable. It can refer to
 * any object that implements the Locatable interface.
 */
Locatable nemo;

nemo = new Fish();    // nemo now refers to an object
                      // of type Fish
nemo.location();      // Calls location () method from
                      // class Fish
nemo = new Mermaid(); // Now, nemo refers to an object
                      // of type Mermaid
nemo.location();      // Calls location() method from
                      // class Mermaid


A variable of type Locatable can refer to any object of any class that implements the Locatable interface. A statement like nemo.location(), above, is legal because nemo is of type Locatable, and any Locatable object has a location() method.

You are not likely to need to write your own interfaces until you get to the point of writing fairly complex programs. However, there are a few interfaces that are used in important ways in Java’s standard packages. You’ll learn about some of these standard interfaces in the next few lessons, and you will see examples of interfaces in the GridWorld Case Study, which was developed for use in AP Computer Science courses by the College Board™.

Last modified: December 12, 2022

Back to Polymorphism

Dark Mode

Outline