14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Interface (Java) 140<br />

Interface (Java)<br />

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic<br />

sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only<br />

contain method signatures and constant declarations (variable declarations that are declared to be both static and<br />

final). An interface may never contain method definitions.<br />

As interfaces are implicitly abstract, they cannot be directly instantiated except when instantiated by a class that<br />

implements the said interface. The class must implement all of the methods described in the interface, or be an<br />

abstract class. Object references in Java may be specified to be of an interface type; in which case, they must either<br />

be null, or be bound to an object that implements the interface.<br />

One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java (other than<br />

java.lang.Object, the root class of the Java type system) must have exactly one base class; multiple inheritance of<br />

classes is not allowed. Furthermore, a Java class may implement, and an interface may extend, any number of<br />

interfaces; however an interface may not implement an interface.<br />

Overview<br />

Interfaces are used to encode similarities which classes of various types share, but do not necessarily constitute a<br />

class relationship. For instance, a human and a parrot can both whistle; however, it would not make sense to<br />

represent Humans and Parrots as subclasses of a Whistler class. Rather they would most likely be subclasses of an<br />

Animal class (likely with intermediate classes), but both would implement the Whistler interface.<br />

Another use of interfaces is being able to use an object without knowing its type of class, but rather only that it<br />

implements a certain interface. For instance, if one were annoyed by a whistling noise, one may not know whether it<br />

is a human or a parrot, because all that could be determined is that a whistler is whistling. In a more practical<br />

example, a sorting algorithm may expect an object of type Comparable. Thus, it knows that the object's type can<br />

somehow be sorted, but it is irrelevant what the type of the object is. The call whistler.whistle() will call the<br />

implemented method whistle of object whistler no matter what class it has, provided it implements Whistler.<br />

For example:<br />

interface Bounceable<br />

{<br />

void setBounce(); // Note the semicolon:<br />

implementations are allowed.<br />

}<br />

Usage<br />

Defining an interface<br />

// Interface methods are public and abstract.<br />

// Think of them as prototypes only; no<br />

Interfaces are defined with the following syntax (compare to Java's class definition):<br />

[visibility] interface InterfaceName [extends other interfaces] {<br />

}<br />

constant declarations<br />

member type declarations<br />

abstract method declarations

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!