An interface defines the outward appearance of an encapsulated class. This refers to the way a class appears to the rest of the program and is not to be confused with a GUI.

Beginning programmers usually encounter their first interface in the context of action events.

To make use of an event the event interface is implemented in the class header as follows:

public class Example extends Applet implements ActionListener

In our Java course students are not expected to write their own interfaces, but if you do need to this example explains how.

public interface ExampleInterface

void interfaceMethod1();


Notice that the word class is missing from the description and the methods within the interface are not declared as public or private, because they are implicitly public.

An interface only contains method names and parameters. This is because it is only an interface for a class. The methods themselves will be written in the class this is the interface for. IN other words an interface only specifies the methods that are needed, the outward appearance of the class.

This diagram summarizes:


In this example the interface Movable is implemented by two classes with their own implementations of the methods. So interfaces allow for programming a selection of classes which all share the same outward appearance, simplifying the structure of a package.

Interfaces and Abstract Classes

It is easy to confuse interfaces with abstract classes. This slideshare provides more detail on these two concepts.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License