14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Java syntax 194<br />

}<br />

AUTUMN {<br />

};<br />

Interfaces<br />

String getDescription() {return "cooler";}<br />

Interfaces are data structures that contain member definitions and not actual implementation. They are useful to<br />

define a contract between members in different types that have different implementations. Every interface is<br />

implicitly abstract. The only modifier allowed to use with interfaces apart from access modifiers is strictfp, which<br />

has the same effect as for classes.<br />

interface ActionListener {<br />

}<br />

int ACTION_ADD = 0;<br />

int ACTION_REMOVE = 1;<br />

void actionSelected(int action);<br />

Implementing an interface<br />

An interface is implemented by a class using the implements keyword. It is allowed to implement more than one<br />

interface, in which case they are written after implements keyword in a comma-separated list. Class implementing an<br />

interface must override all its methods, otherwise it must be declared as abstract.<br />

interface RequestListener {<br />

}<br />

int requestReceived();<br />

class ActionHandler implements ActionListener, RequestListener {<br />

}<br />

void actionSelected(int action) {<br />

}<br />

public int requestReceived() {<br />

}<br />

//Calling method defined by interface<br />

RequestListener listener = new ActionHandler(); /*ActionHandler can be<br />

represented as RequestListener...*/<br />

listener.requestReceived(); /*...and thus is known to implement<br />

requestReceived() method*/

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

Saved successfully!

Ooh no, something went wrong!