Sub topic 9
What Cannot Be Overridden: static, private, and final
In earlier chapters, you saw that Java picks an overridden method at runtime, based on the object's real class. Some methods do not work this way. Methods marked static, private, or final are never chosen at runtime. This chapter shows what happens with each one.
Static Methods Are Hidden, Not Overridden
A static method is a method that belongs to the class itself, not to any one object. You call it without needing an object.
If a subclass writes a static method with the same name and parameters, it does not override the parent's method. It hides it. Hiding means that Java picks the method from the variable's declared type, the type written in the code, and ignores the real object.
class Parent {
static String kind() {
return "Parent.kind";
}
String who() {
return "Parent.who";
}
}
class Child extends Parent {
static String kind() {
return "Child.kind";
}
@Override
String who() {
return "Child.who";
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.kind()); // Parent.kind
System.out.println(p.who()); // Child.who
System.out.println(Child.kind()); // Child.kind
}
}The variable p holds a Child object. The call p.who() runs Child.who because who() is an instance method. The call p.kind() runs Parent.kind because p is declared as Parent, and Java decides static calls at compile time.
You also cannot put @Override on a static method. The compiler rejects it, because nothing is being overridden.
Private Methods Are Invisible to Subclasses
A private method can only be used inside its own class. A subclass cannot see it, so it cannot override it.
If a subclass writes a method with the same name, that is a brand-new, separate method. The two methods have no connection.
class Vault {
private String secret() {
return "Vault secret";
}
String reveal() {
return secret(); // always calls Vault's secret()
}
}
class MyVault extends Vault {
// A new method, not an override.
// Adding @Override here would cause a compile error.
String secret() {
return "MyVault secret";
}
}
public class Main {
public static void main(String[] args) {
Vault v = new MyVault();
System.out.println(v.reveal()); // Vault secret
}
}Final Methods Are Locked
A final method is a method that subclasses are not allowed to override. You use final when a method must always behave the same way, for example a safety check.
class Door {
final boolean isLocked() {
return true;
}
}
class GlassDoor extends Door {
// This would not compile:
// boolean isLocked() { return false; }
// error: isLocked() in GlassDoor cannot override isLocked() in Door
}A subclass still inherits and can call isLocked(). It just cannot replace it.
Summary Table
| Method kind | Can a subclass replace it? | Which version runs? |
|---|---|---|
| Normal instance method | Yes, by overriding | The object's real class |
| `static` | No, it can only hide it | The declared type |
| `private` | No, it is not visible | The class that defines it |
| `final` | No, the compiler stops you | The class that defines it |
Exercise
Given Parent p = new Child(); where both classes define static String kind(), what does p.kind() return, and why?
Show answer
It returns the result of Parent.kind(). Static methods are hidden, not overridden, so Java picks the method from the declared type Parent, not from the real Child object.
Exercise
A subclass declares a method with the same name and parameters as a private method in its parent and marks it with @Override. What happens?
Show answer
The code does not compile. The parent's private method is not visible to the subclass, so there is nothing to override.