Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


What Are Interfaces?


Definition: An interface is a collection of method definitions (without implementations) and constant values.

You use interfaces to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. A better approach to the spreadsheet cell problem would be to create an interface, say CellAble, that defined the list of methods that a "cell value" must implement: toString, draw, toFloat, and so on. Then the spreadsheet cells can contain any type of object that implements the CellAble interface (or in other words, implements the list of methods). Objects that implement the CellAble interface do not have to be hierarchically related.

Interfaces are useful for:

In Java, an interface is a reference data type and, as such, can be used in many of the same places where a type can be used (such as in method arguments and variable declarations). You'll see how to do this in Using an Interface as a Type.

Interfaces Do Not Provide Multiple Inheritance

Often interfaces are touted as an alternative to multiple class inheritance. While interfaces may solve some of the same problems as multiple class inheritance, they are quite different animals. In particular:


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces