Sub topic 10
Static Methods Are Hidden, Not Overridden
Assume you already know how to define classes, create objects with new, call instance methods, and use basic inheritance. This chapter explains a special inheritance rule: Java overrides instance methods, but it hides static methods.
Overriding and dynamic dispatch
An instance method belongs to an object. When a subclass provides a new implementation, it overrides the method. Java chooses the implementation at runtime from the object's actual type.
A static method belongs to a class. It does not use an object, so Java chooses the method from the reference's declared type at compile time. A subclass method with the same signature hides the superclass method.
class Animal {
static String describe() {
return "Animal static method";
}
String name() {
return "Animal instance method";
}
}
class Dog extends Animal {
static String describe() {
return "Dog static method";
}
@Override
String name() {
return "Dog instance method";
}
}
public class HidingDemo {
public static void main(String[] args) {
Animal animal = new Dog();
System.out.println(animal.name());
System.out.println(animal.describe());
System.out.println(Dog.describe());
System.out.println(Animal.describe());
}
}The output is:
Dog instance method
Animal static method
Dog static method
Animal static methodThe object created by new Dog() controls the instance call, so animal.name() uses Dog.name(). The variable animal has the declared type Animal, so animal.describe() uses Animal.describe().
The class name makes the static choice clear. Prefer Dog.describe() or Animal.describe() instead of calling a static method through an object reference.
How method selection works
| Member kind | Selected using | Inheritance behavior |
|---|---|---|
| Instance method | Runtime object type | Overridden |
| Static method | Compile-time declared type | Hidden |
| Static method called with a class name | Named class | Explicit and predictable |
This distinction matters when code is refactored. Changing a variable's declared type can change which static method a call uses, even when the object stored in the variable does not change.
A static method can be hidden only when the subclass can inherit the method and declares a compatible static method. A subclass cannot turn a static method into an instance method, or an instance method into a static method. Java rejects those declarations because they would make method lookup ambiguous.
Avoiding confusing calls
Calling a static method through an instance reference is legal in many cases, but it is misleading:
Animal animal = new Dog();
System.out.println(animal.describe()); // Animal.describe()The expression looks as if the Dog object should affect the result. It does not. Use the class name:
System.out.println(Animal.describe());Static methods also cannot participate in runtime polymorphism. If a program needs each subclass to supply different behavior through a superclass reference, define an instance method instead.
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
private final double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
}Here, area() is an instance method because the result depends on each shape object's state.
A useful mental model
Think of an instance call as asking an object, “How do you perform this operation?” Think of a static call as asking a named class, “What implementation does this class provide?” The two questions use different lookup rules.
Exercise
If Animal animal = new Dog();, which methods run for animal.name() and animal.describe() when name() is an instance method and describe() is static?
Show answer
Dog.name() runs because instance methods are overridden and selected from the runtime object. Animal.describe() runs because static methods are hidden and selected from the declared type.