Java Reference
In-Depth Information
If a method is declared to have a return type, then every return statement (§ 14.17 ) in its body
must have an Expression , or a compile-time error occurs.
If a method is declared to have a return type, then a compile-time error occurs if the body
of the method can complete normally (§ 14.1 ).
In other words, a method with a return type must return only by using a return state-
ment that provides a value return; it is not allowed to “drop off the end of its body”.
Note that it is possible for a method to have a declared return type and yet contain no
return statements. Here is one example:
Click here to view code image
class DizzyDean {
int pitch() { throw new RuntimeException("90 mph?!"); }
}
8.4.8. Inheritance, Overriding, and Hiding
A class C inherits from its direct superclass and direct superinterfaces all abstract and
non- abstract methods of the superclass and superinterfaces that are public , protected , or de-
clared with default access in the same package as C , and are neither overridden (§ 8.4.8.1 )
nor hidden (§ 8.4.8.2 ) by a declaration in the class.
Methods are overridden or hidden on a signature-by-signature basis.
If, for example, a class declares two public methods with the same name (§ 8.4.9 ), and
a subclass overrides one of them, the subclass still inherits the other method.
If the method not inherited is declared in a class, or the method not inherited is declared in
an interface and the new declaration is abstract , then the new declaration is said to override
it.
If the method not inherited is abstract and the new declaration is not abstract , then the new
declaration is said to implement it.
8.4.8.1. Overriding (by Instance Methods)
An instance method m 1 , declared in class C , overrides another instance method m 2 , de-
clared in class A iff all of the following are true:
C is a subclass of A .
• The signature of m 1 is a subsignature (§ 8.4.2 ) of the signature of m 2 .
• Either:
Search WWH ::




Custom Search