Java Reference
In-Depth Information
Eachof Listing2-45 's A and B interfacesdeclaresaconstantnamed X .Despiteeach
constant havingthesame typeandvalue, thecompiler will reportanerrorwheniten-
counters X in Collision 'ssecond foo() methodbecauseitdoesnotknowwhich X
is being inherited.
Speakingof foo() ,thecompilerreportsanerrorwhenitencounters Collision 's
second foo() declarationbecause foo() hasalreadybeendeclared.Youcannotover-
load a method by changing only its return type.
has this to say when told to compile Listing 2-45 : error: foo() is already defined in Col-
public int foo() { return X; }
^ error: Collision is not abstract and
does not override abstract
foo() in B
class Collision implements A, B
^ error: foo() in Collision cannot imple-
ment foo() in B
public void foo();
return type void is not compatible with int error: reference to X is ambiguous, both
variable X in A and
variable X
in B match
public int foo() { return X; }
4 errors
Extending Interfaces
Justasasubclasscanextendasuperclassviareservedword extends ,youcanusethis
reservedwordtohavea subinterface extenda superinterface .This,too,isknownas in-
terface inheritance .
Search WWH ::

Custom Search