FAQ

Java

JSP

Servlet


Advertisement



Can static methods be overridden?

 

The static methods can not be overridden!

If a subclass defines a static method with the same signature as a static method in the superclass, the method in the subclass hides the one in the superclass. The distinction between hiding and overriding has important implications.


If a class declares a static method m, then the declaration m is said to hide any method m', where the signature of m is a subsignature of the signature of m', 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, for it is permissible for a static variable to hide an instance variable. Hiding is also distinct from shadowing and obscuring.

A hidden method can be accessed by using a qualified name or by using a method invocation expression that contains the keyword super or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields.

[8.4.8.2 Hiding (by Class Methods) in Java Language Specification 3rd Edition]


Let's look at an example. This example contains two classes. The first is Animal, which contains one instance method and one class method:

public class Animal {
public static void hide() {
System.out.format("The hide method in Animal.%n");
}
public void override() {
System.out.format("The override method in Animal.%n");
}
}

The second class, a subclass of Animal, is called Cat:

public class Cat extends Animal {
public static void hide() {
System.out.format("The hide method in Cat.%n");
}
public void override() {
System.out.format("The override method in Cat.%n");
}

public static void main(String[] args) {
Cat myCat = new Cat();
System.out.println("Create a Cat instance ...");
myCat.hide();
Cat.hide();
myCat.override();

Animal myAnimal = myCat;
System.out.println("\nCast the Cat instance to Animal...");
myAnimal.hide(); //BAD STYLE
Animal.hide(); //Better!
myAnimal.override();

Animal myAnimal1 = new Animal();
System.out.println("\nCreate an Animal instance....");
myAnimal1.hide(); //BAD STYLE
Animal.hide(); //Better!
myAnimal1.override();
}
}

The output results are:

Create a Cat instance ...
The hide method in Cat.
The hide method in Cat.
The override method in Cat.

Cast the Cat instance to Animal...
The hide method in Animal.
The hide method in Animal.
The override method in Cat.

Create an Animal instance....
The hide method in Animal.
The hide method in Animal.
The override method in Animal.

There are three sections in this example. The first and third ones are normal. The second one is wired one, we cast the Cat type instance to it's parent Animal type. myAnimal.hide() invoke the static method defined in the Animal class.  If this method were truly overridden, we should have invoked static hide() method defined in Cat class, but we didn't.

It is considered bad style to call static methods on instances because hiding can be confusing. So it is better to use the class, for example Animal.hide or Cat.hide. The output from this program is as follows:

The hide method in Animal.
The override method in Cat.

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 other words, call to static methods are mapped at the compile time and depends on the declared type of the reference(Parent in this case) and not the instance the reference points at runtime. In the example, the compile-time type of myAnimal is Animal. Thus, the runtime system invokes the hide method defined in Animal.

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 of myAnimal is Cat. Thus, the runtime system invokes the override method defined in Cat.

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.

How to access the static methods defined in the superclass?

A hidden method can be accessed by using a qualified name or by using a method invocation expression that contains the keyword super or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields.

public class Cat extends Animal {
public static void hide() {
System.out.format("The hide method in Cat.%n");
}
public void override() {
hide(); //Access Cat's hide() method
super.hide(); //Access Animal's hide() method
Animal.hide(); //Access Animal's hide() method
System.out.format("The override method in Cat.%n");
}

public static void main(String[] args) {
....
}
}


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |