The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type.
When overriding a method, you might want to use
the @Override
annotation that instructs the
compiler that you intend to override a method in the superclass.
If, for some reason, the compiler detects that the method does
not exist in one of the superclasses, it will generate an
error. For more information on @Override
, see
Annotations
.
The distinction between hiding and overriding has important
implications. The version of the overridden
method that gets invoked is the one in the subclass. The version of the
hidden method that gets invoked depends on whether it is invoked from the superclass
or the subclass.
Let's look at an example that contains two classes. The first is Animal
,
which contains one instance method and one class method:
public class Animal { public static void testClassMethod() { System.out.println("The class method in Animal."); } public void testInstanceMethod() { System.out.println("The instance method in Animal."); } }
Animal
,
is called Cat
:
public class Cat extends Animal { public static void testClassMethod() { System.out.println("The class method in Cat."); } public void testInstanceMethod() { System.out.println("The instance method in Cat."); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = myCat; Animal.testClassMethod(); myAnimal.testInstanceMethod(); } }
Cat
class overrides the instance method
in Animal
and
hides the class method in Animal
.
The main
method in this
class creates an instance of Cat
and calls
testClassMethod()
on the class and testInstanceMethod()
on the instance.
The output from this program is as follows:
The class method in Animal. The instance method in Cat.
You will get a compile-time error if you attempt to change an instance method in the superclass to a class method in the subclass, and vice versa.
Superclass Instance Method | Superclass Static Method | |
---|---|---|
Subclass Instance Method | Overrides | Generates a compile-time error |
Subclass Static Method | Generates a compile-time error | Hides |