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.
Thecompilerwillprobablyreportadditionalerrors.Forexample,theJava7compiler
has this to say when told to compile Listing 2-45 :
Collision.java:16: error: foo() is already defined in Col-
lision
public int foo() { return X; }
^
Collision.java:11: error: Collision is not abstract and
does not override abstract
method
foo() in B
class Collision implements A, B
^
Collision.java:14: error: foo() in Collision cannot imple-
ment foo() in B
public void foo();
^
return type void is not compatible with int
Collision.java:16: 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