10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CLASSES Inheritance, Overriding, and Hiding 8.4.8<br />

It is a compile time error if a type declaration T has a member method m1 and<br />

there exists a method m2 declared in T or a supertype of T such that all of the following<br />

conditions hold:<br />

• m1 and m2 have the same name.<br />

• m2 is accessible from T.<br />

• <strong>The</strong> signature of m1 is not a subsignature (§8.4.2) of the signature of m2. • m1 or some method m1 overrides (directly or indirectly) has the same erasure<br />

as m2 or some method m2 overrides (directly or indirectly).<br />

DISCUSSION<br />

<strong>The</strong>se restrictions are necessary because generics are implemented via erasure. <strong>The</strong> rule<br />

above implies that methods declared in the same class with the same name must have different<br />

erasures. It also implies that a type declaration cannot implement or extend two distinct<br />

invocations of the same generic interface. Here are some further examples.<br />

A class cannot have two member methods with the same name and type erasure.<br />

class C { T id (T x) {...} }<br />

class D extends C {<br />

Object id(Object x) {...}<br />

}<br />

This is illegal since D.id(Object) is a member of D, C.id(String) is<br />

declared in a supertype of D and:<br />

• <strong>The</strong> two methods have the same name, id<br />

• C.id(String) is accessible to D<br />

DRAFT<br />

• <strong>The</strong> signature of D.id(Object) is not a subsignature of that of<br />

C.id(String)<br />

• <strong>The</strong> two methods have the same erasure<br />

DISCUSSION<br />

Two different methods of a class may not override methods with the same erasure.<br />

class C { T id (T x) {...} }<br />

interface I { Tid(T x); }<br />

class D extends C implements I {<br />

227

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!