Java Reference
In-Depth Information
interface Drawable
int RED = 1;
// For simplicity, integer constants are
used. These
int GREEN = 2; // constants are not that descriptive, as
you will see.
int BLUE = 3;
int BLACK = 4;
void draw(int color);
Listing2-42 declaresaninterface named Drawable .Byconvention,aninterface's
multiword interface name is capitalized.
Note Manyinterfacenamesendwiththe able suffix.Forexample,Java'sstandard
class library includes interfaces named Adjustable , Callable , Comparable ,
Cloneable , Iterable , Runnable , and Serializable . It's not mandatory
to use this suffix; the standard class library also provides interfaces named
CharSequence , Collection , Composite , Executor , Future , Iterator ,
List , Map , and Set .
Drawable declares four fields that identify color constants. Drawable also de-
claresa draw() methodthatmustbecalledwithoneoftheseconstantstospecifythe
color used to draw something.
Note Youcanprecede interface with public ,tomakeyourinterfaceaccess-
ibletocodeoutsideofitspackage.(Iwilldiscusspackagesin Chapter3 . )Otherwise,
the interface is only accessible to other types in its package. You can also precede
interface with abstract ,toemphasizethataninterfaceisabstract.Becausean
interface is already abstract, it is redundant to specify abstract in the interface's
declaration.Aninterface'sfieldsareimplicitlydeclared public , static ,and fi-
nal . It is therefore redundant to declare them with these reserved words. Because
these fields are constants, they must be explicitly initialized; otherwise, the compiler
reports an error. Finally, an interface's methods are implicitly declared public and
abstract .Therefore,itisredundanttodeclarethemwiththesereservedwords.Be-
cause these methods must be instance methods, do not declare them static or the
compiler will report errors.
Search WWH ::

Custom Search