O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining and Implementing an Interface

An interface is a set of requirements to which classes must conform if they want to use the service provided by the interface. To implement an interface, first declare that your class will implement the given interface; second, define the methods in the interface.

java.util.Collection has an interface that defines, among other methods, an add() method. This ensures that any implementation of java.util.Collection will support its methods.

An interface is defined as follows:

public interface Collection {
public abstract boolean add(A o)
...
}

An interface is implemented as follows:

 public class myClass implements Collection { //code here. can write, for instance, add() since //add() is a method of java.util.Collection, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required