OO Programming with Java


Interfaces

  1. As a variation, a specification of a set of operations to be "implemented" can be more easily defined by the way of an interface:
public interface Element {  public int count();  }
public abstract class NamedElement implements Element { ... }

As a consequence, if a class can only extends a single class, it can implement many interfaces.

  1. There are many interface that can be used to integrate user defined class into Java API with for instance:
interface Comparable { int compare(Object o); }          // use to sort collection
interface Iterator  { boolean hasNext(); Object next(); }// use to loop on collections
interface Iterable  { Iterator iterator(); }      // complementary of the previous one
interface Runnable  { void run(); }                      // for flow of control
                                                         // and multitasking
...

UML/SysML class diagram


11 - 14