Java Reference
In-Depth Information
For example, the duplicate color constants in Drawable and Fillable lead to
namecollisionswhenyouspecifytheirnamesbythemselvesinanimplementingclass.
Toavoidthesenamecollisions,prefixanamewithitsinterfacenameandthemember
access operator, or place these constants in their own interface, and have Drawable
and Fillable extend this interface, as demonstrated in Listing 2-46 .
Listing 2-46. Extending the Colors interface
interface Colors
{
int RED = 1;
int GREEN = 2;
int BLUE = 3;
int BLACK = 4;
}
interface Drawable extends Colors
{
void draw(int color);
}
interface Fillable extends Colors
{
void fill(int color);
}
Thefactthat Drawable and Fillable eachinheritconstantsfrom Colors isnot
aproblemforthecompiler.Thereisonlyasinglecopyoftheseconstants(in Colors )
and no possibility of a name collision, and so the compiler is satisfied.
If a class can implement multiple interfaces by declaring a comma-separated list of
interfacenamesafter implements ,itseemsthataninterfaceshouldbeabletoextend
multiple interfaces in a similar way. This feature is demonstrated in Listing 2-47 .
Listing 2-47. Extending a pair of interfaces
interface A
{
int X = 1;
}
interface B
 
Search WWH ::




Custom Search