Sub topic 10
Why Fields Don't Behave Polymorphically
You have seen that Java picks an overridden method based on the object's real type. Fields work differently. This chapter shows how, and why it matters.
Methods vs fields
A field is a variable that belongs to an object, like name or age. When a subclass declares a field with the same name as a field in its parent, the new field does not replace the old one. It hides it. Hiding means both fields exist in the object, and Java chooses one based on the variable's declared type.
Here is the key rule:
- For an instance method, Java looks at the runtime type, which is the class of the actual object.
- For a field, Java looks at the declared type, which is the type written next to the variable.
A worked example
class Parent {
String name = "Parent";
String who() {
return "Parent.who";
}
}
class Child extends Parent {
String name = "Child";
@Override
String who() {
return "Child.who";
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
Child c = new Child();
System.out.println(p.name); // Parent
System.out.println(p.who()); // Child.who
System.out.println(c.name); // Child
}
}Both p and c point to a Child object. But p is declared as Parent, so p.name reads the Parent field. The method call p.who() still runs the Child version, because methods are dispatched at runtime.
Why Java works this way
The compiler decides which field to use while it compiles your code. At that point it only knows the declared type. It does not know what object will be stored there later. Methods get an extra lookup step at runtime, and fields do not.
The fix: use methods to reach fields
If you want behavior that changes with the object's real type, put it in a method. A getter is a small method that returns a field's value. Getters can be overridden, so they are polymorphic.
class Parent {
String getName() {
return "Parent";
}
}
class Child extends Parent {
@Override
String getName() {
return "Child";
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.getName()); // Child
}
}Good habits:
- Make fields
private. Then subclasses cannot see them, and hiding cannot confuse callers. - Read values through methods when subclasses may need to change them.
- Do not reuse a parent's field name in a subclass.
| Member | Chosen by | Polymorphic? |
|---|---|---|
| Instance method | Runtime type | Yes |
| Field | Declared type | No |
Exercise
Given Parent p = new Child(); with the first example's classes, what does System.out.println(((Child) p).name); print, and why?
Show answer
It prints "Child". The cast changes the declared type of the expression to Child, and Java picks the field from the declared type.
Exercise
You want p.name to show "Child" when p holds a Child object. What should you change?
Show answer
Make the field private and add a getName() method in Parent that Child overrides. Then call p.getName(). Java picks the method at runtime, so it returns "Child".