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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CLASSES Member Type Declarations 8.5<br />

This example results in a compile-time error, because the override of method<br />

move in class CheckedPoint declares that it will throw a checked exception that<br />

the move in class Point has not declared. If this were not considered an error, an<br />

invoker of the method move on a reference of type Point could find the contract<br />

between it and Point broken if this exception were thrown.<br />

Removing the throws clause does not help:<br />

class CheckedPoint extends Point {<br />

void move(int dx, int dy) {<br />

if ((x + dx) < 0 || (y + dy) < 0)<br />

throw new BadPointException();<br />

x += dx; y += dy;<br />

}<br />

}<br />

A different compile-time error now occurs, because the body of the method<br />

move cannot throw a checked exception, namely BadPointException, that does<br />

not appear in the throws clause for move.<br />

8.5 Member Type Declarations<br />

A member class is a class whose declaration is directly enclosed in another class<br />

or interface declaration. Similarly, a member interface is an interface whose declaration<br />

is directly enclosed in another class or interface declaration. <strong>The</strong> scope<br />

(§6.3) of a member class or interface is specified in §8.1.6.<br />

If the class declares a member type with a certain name, then the declaration<br />

of that type is said to hide any and all accessible declarations of member types<br />

with the same name in superclasses and superinterfaces of the class.<br />

Within a class C, a declaration d of a member type named n shadows the declarations<br />

of any other types named n that are in scope at the point where d occurs.<br />

If a member class or interface declared with simple name C is directly<br />

enclosed within the declaration of a class with fully qualified name N, then the<br />

member class or interface has the fully qualified name N.C. A class inherits from<br />

its direct superclass and direct superinterfaces all the non-private member types of<br />

the superclass and superinterfaces that are both accessible to code in the class and<br />

not hidden by a declaration in the class.<br />

A class may inherit two or more type declarations with the same name, either<br />

from two interfaces or from its superclass and an interface. A compile-time error<br />

occurs on any attempt to refer to any ambiguously inherited class or interface by<br />

its simple name<br />

DRAFT<br />

237

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

Saved successfully!

Ooh no, something went wrong!