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.

4.4 Type Variables TYPES, VALUES, AND VARIABLES<br />

50<br />

Type variables have an optional bound, T & I 1 ... I n. <strong>The</strong> bound consists of<br />

either a type variable, or a class or interface type T possibly followed by further<br />

interface types I 1 , ..., I n. If no bound is given for a type variable, Object is<br />

assumed. It is a compile-time error if any of the types I 1 ... I n is a class type or<br />

type variable. <strong>The</strong> erasures (§4.6) of all constituent types of a bound must be pairwise<br />

different, or a compile-time error occurs. <strong>The</strong> order of types in a bound is<br />

only significant in that the erasure of a type variable is determined by the first type<br />

in its bound, and that a class type or type variable may only appear in the first<br />

position.<br />

A type variable may not at the same time be a subtype of two interface types<br />

which are different parameterizations of the same generic interface.<br />

See section §6.3 for the rules defining the scope of type variables.<br />

<strong>The</strong> members of a type variable X with bound T & I 1 ... I n are the members<br />

of the intersection type (§4.9) T & I 1 ... I n appearing at the point where the type<br />

variable is declared.<br />

DISCUSSION<br />

<strong>The</strong> following example illustrates what members a type variable has.<br />

package TypeVarMembers;<br />

class C {<br />

void mCDefault() {}<br />

public void mCPublic() {}<br />

private void mCPrivate() {}<br />

protected void mCProtected() {}<br />

}<br />

class CT extends C implements I {}<br />

interface I {<br />

void mI(); }<br />

void test(T t) {<br />

t.mI(); // OK<br />

t.mCDefault(); // OK<br />

t.mCPublic(); // OK<br />

t.mCPrivate(); // compile-time error<br />

t.mCProtected(); // OK<br />

}<br />

}<br />

<strong>The</strong> type variable T has the same members as the intersection type C & I, which in<br />

turn has the same members as the empty class CT, defined in the same scope with equivalent<br />

supertypes. <strong>The</strong> members of an interface are always public, and therefore always<br />

inherited (unless overridden). Hence mI is a member of CT and of T. Among the members<br />

of C, all but mCPrivate are inherited by CT, and are therefore members of both CT and T.<br />

DRAFT

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

Saved successfully!

Ooh no, something went wrong!