Java Reference
In-Depth Information
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:
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-
nor hidden (§
8.4.8.2
) by a declaration in the class.
Methods are overridden or hidden on a signature-by-signature basis.
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
.
• Either: