Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
An instance method in a subclass with the same signature and return type as an instance method in the superclass overrides the superclass's method. (Remember that a method's signature is its name and the number and the type of its arguments.) 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. For example, theObject
class contains an instance method calledtoString
that returns a string representation of an instance of that type. Every class inherits this method. The implementation inObject
is not very useful for subclasses; thus, we recommend overriding this method to provide better information for your classes. It's particularly useful for debugging. Here's an example of overridingtoString
:The overriding method has the same name, number and type of arguments, and return value as the method it overrides. (In fact the return type of a subclass can be a subclass of the return type of its superclass. See the section The clone Method for an example.) The overriding method can have a differentpublic class MyClass { private int anInt = 4; //Overrides toString in Object class. public String toString() { return "Instance of MyClass. anInt = " + anInt; } }throws
clause as long as it doesn't specify any types not specified by thethrows
clause in the overridden method. Also, the access specifier for the overriding method can allow more but not less access than the overridden method. For example, a protected method in the superclass can be made public but not private.A subclass cannot override methods that are declared
final
in the superclass (by definition, final methods cannot be overridden). If you attempt to override a final method, the compiler displays an error message. The section Writing Final Classes and Methods discusses final methods in detail.A subclass must override methods that are declared
abstract
in the superclass, or the subclass itself must be abstract. The section Writing Abstract Classes and Methods discusses abstract classes and methods in detail. Recall from Naming a Method that the Java programming language allows you to overload methods by changing the number or the type of arguments to the method. You can also overload methods in a superclass. Here's an example of overloading thetoString
method:As illustrated by the preceding example, you might overload a superclass method to provide additional functionality. When writing a method that has the same name as a method in a superclass, double check the return values and the argument lists to make sure that you are overloading or overriding as you intended.public class MyClass { private int anInt = 4; //Overrides toString in Object class. public String toString() { return "Instance of MyClass. anInt = " + anInt; } //Overloads toString method name to provide //additional functionality. public String toString(String prefix) { return prefix + ": " + toString(); } }If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass. The distinction between hiding and overriding has important implications. Let's look at an example to see why. This example contains two classes. The first is
Animal
, which contains one instance method and one class method:The second class, a subclass ofpublic class Animal { public static void hide() { System.out.println("The hide method in Animal."); } public void override() { System.out.println("The override method in Animal."); } }Animal
, is calledCat
:Thepublic class Cat extends Animal { public static void hide() { System.out.println("The hide method in Cat."); } public void override() { System.out.println("The override method in Cat."); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = (Animal)myCat; myAnimal.hide(); myAnimal.override(); } }Cat
class overrides the instance method inAnimal
calledoverride
and hides the class method inAnimal
calledhide
. Themain
method in this class creates an instance ofCat
, casts it to aAnimal
reference, and then calls both thehide
and theoverride
methods on the instance. The output from this program is as follows:The version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass. For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called. In the example, the compile-time type ofThe hide method in Animal. The override method in Cat.myAnimal
isAnimal
. Thus, the runtime system invokes thehide
method defined inAnimal
. For instance methods, the runtime system invokes the method defined in the runtime type of the reference on which the method is called. In the example, the runtime type ofmyAnimal
isCat
. Thus, the runtime system invokes theoverride
method defined inCat
.An instance method cannot override a static method, and a static method cannot hide an instance method. The following table summarizes what happens when you define a method with the same signature as a method in a superclass.
Defining a Method with the Same Signature as a Superclass's Method Superclass Instance Method Superclass Static Method Instance Method Overrides (must also have the same return type) Generates a compile-time error Static Method Generates a compile-time error Hides
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.