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 Inheritance, Overriding, and Hiding 8.4.8<br />

A compile-time error occurs if an instance method overrides a static<br />

method.<br />

In this respect, overriding of methods differs from hiding of fields (§8.3), for<br />

it is permissible for an instance variable to hide a static variable.<br />

An overridden method can be accessed by using a method invocation expression<br />

(§15.12) that contains the keyword super. Note that a qualified name or a<br />

cast to a superclass type is not effective in attempting to access an overridden<br />

method; in this respect, overriding of methods differs from hiding of fields. See<br />

§15.12.4.9 for discussion and examples of this point.<br />

<strong>The</strong> presence or absence of the strictfp modifier has absolutely no effect on<br />

the rules for overriding methods and implementing abstract methods. For example,<br />

it is permitted for a method that is not FP-strict to override an FP-strict<br />

method and it is permitted for an FP-strict method to override a method that is not<br />

FP-strict.<br />

8.4.8.2 Hiding (by Class Methods)<br />

If a class declares a static method m, then the declaration m is said to hide any<br />

method m’, where the signature of m is a subsignature (§8.4.2) of the signature of<br />

m’, in the superclasses and superinterfaces of the class that would otherwise be<br />

accessible to code in the class. A compile-time error occurs if a static method<br />

hides an instance method.<br />

In this respect, hiding of methods differs from hiding of fields (§8.3), for it is<br />

permissible for a static variable to hide an instance variable. Hiding is also distinct<br />

from shadowing (§6.3.1) and obscuring (§6.3.2).<br />

A hidden method can be accessed by using a qualified name or by using a<br />

method invocation expression (§15.12) that contains the keyword super or a cast<br />

to a superclass type. In this respect, hiding of methods is similar to hiding of<br />

fields.<br />

DRAFT<br />

8.4.8.3 Requirements in Overriding and Hiding<br />

If a method declaration d1 with return type R1 overrides or hides the declaration of<br />

another method d2 with return type R2, then d1 must be return-type substitutable<br />

for d2, or a compile-time error occurs. Furthermore, if R1 is not a subtype of R2,<br />

an unchecked warning must be issued (unless suppressed (§9.6.1.5)).<br />

A method declaration must not have a throws clause that conflicts (§8.4.6)<br />

with that of any method that it overrides or hides; otherwise, a compile-time error<br />

occurs.<br />

225

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

Saved successfully!

Ooh no, something went wrong!