A class inherits from its direct superclass and direct superinterfaces all the methods (whether abstract
or not) of the superclass and superinterfaces that are
accessible to code in the class and are neither overridden (§8.4.6.1) nor hidden
(§8.4.6.2) by a declaration in the class.
If a class declares an instance method, then the declaration of that method is said
to override any and all methods with the same signature in the superclasses and
superinterfaces of the class that would otherwise be accessible to code in the class.
Moreover, if the method declared in the class is not abstract
, then the declaration of that method is said to implement any and all declarations of abstract
methods with the same signature in the superclasses and superinterfaces of the
class that would otherwise be accessible to code in the class.
A compile-time error occurs if an instance method overrides a static
method. In this respect, overriding of methods differs from hiding of fields (§8.3), for it is permissible for an instance variable to hide a static
variable.
An overridden method can be accessed by using a method invocation expression (§15.11) that contains the keyword super
. Note that a qualified name or a cast to a superclass type is not effective in attempting to access an overridden method; in this respect, overriding of methods differs from hiding of fields. See §15.11.4.10 for discussion and examples of this point.
If a class declares a static
method, then the declaration of that method is said to
hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A
compile-time error occurs if a static
method hides an instance method. In this
respect, hiding of methods differs from hiding of fields (§8.3), for it is permissible
for a static
variable to hide an instance variable.
A hidden method can be accessed by using a qualified name or by using a method invocation expression (§15.11) that contains the keyword super
or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields.
If a method declaration overrides or hides the declaration of another method, then
a compile-time error occurs if they have different return types or if one has a
return type and the other is void
. Moreover, a method declaration must not have a
throws
clause that conflicts (§8.4.4) with that of any method that it overrides or
hides; otherwise, a compile-time error occurs. In these respects, overriding of
methods differs from hiding of fields (§8.3), for it is permissible for a field to hide
a field of another type.
The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:
public
, then the overriding or hiding method must be public
; otherwise, a compile-time error occurs.
protected
, then the overriding or hiding method must be protected
or public
; otherwise, a compile-time error occurs.
private
; otherwise, a compile-time error occurs.
Note that a private
method is never accessible to subclasses and so cannot be
hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private
method in one of
its superclasses, and there is no requirement that the return type or throws
clause
of such a method bear any relationship to those of the private
method in the
superclass.
It is possible for a class to inherit more than one method with the same signature (§8.4.6.4). Such a situation does not in itself cause a compile-time error. There are then two possible cases:
abstract
, then there are two subcases:
abstract
is static
, a compile-time error occurs.
abstract
is considered to override, and therefore to implement, all the other methods on behalf of the class that inherits it. A compile-time error occurs if, comparing the method that is not abstract
with each of the other of the inherited methods, for any such pair, either they have different return types or one has a return type and the other is void
. Moreover, a compile-time error occurs if the inherited method that is not abstract
has a throws
clause that conflicts (§8.4.4) with that of any other of the inherited methods.
abstract
, then the class is necessarily an abstract
class and is considered to inherit all the abstract
methods. A compile-time error occurs if, for any two such inherited methods, either they have different return types or one has a return type and the other is void
. (The throws
clauses do not cause errors in this case.)
It is not possible for two or more inherited methods with the same signature not to
be abstract
, because methods that are not abstract
are inherited only from the
direct superclass, not from superinterfaces.
There might be several paths by which the same method declaration might be inherited from an interface. This fact causes no difficulty and never, of itself, results in a compile-time error.