Lesson · Inheritance & Polymorphism
What is not overriding: look-alikes that surprise you
16 min readWhat Is Not Overriding
Fields shadow, static methods hide, private methods never join the contract, and same name with new parameters is just an overload. Only a real override replaces behaviour at runtime; the look-alikes are decided by the compiler.
The field that refuses to change
Both classes declare a field with the same name, and the child object is asked for it through a parent-typed reference:
class Parent { String name = "parent"; } class Child extends Parent { String name = "child"; } Parent p = new Child(); System.out.println(p.name); // predict before reading on
Predict: the object is a Child, and its field says "child". Yet this prints "parent". Fields are not methods: they do not dispatch, and the reference's declared type picks which slot is read. Worse, both fields exist inside the object at the same time.
The five look-alikes, one table
Only the last row is a real override. Each look-alike is decided by the compiler, using the types written in the source:
| Look-alike | What actually happens | Decided by |
|---|---|---|
| Same field name in both classes | Both fields live in every child object; reading gives the one on the reference's declared type (shadowing) | Compiler: reference type |
| Same static method signature | The child's method hides the parent's; the call is bound from the reference type | Compiler: reference type |
Same private method signature | Nothing is inherited, so nothing is replaced; the parent's own calls keep using the parent's method | Compiler: where the call is written |
| Same name, different parameters | An overload: a new method, chosen at compile time from the argument types | Compiler: argument types |
| Same name, same parameters, inherited | A real override: the object's class decides which body runs | Runtime: the object |
One question sorts every case: does an object take part? A real override asks the object at runtime. Every look-alike resolves from what the source file wrote, because no choice survives to runtime.
Why fields and statics behave differently
The split is deliberate, and knowing the reason stops it from feeling random:
- Methods describe behaviour; behaviour is what inheritance wants to vary. Dispatching a method costs one lookup and buys polymorphism; the language pays it for methods.
- Fields are plain slots. Making every field read consult the object's class would tax all data access for a case that is nearly always a mistake anyway: avoid shadowing, and the question never arises.
- Statics have no object to dispatch to. A static call carries only the class or reference you wrote it on, so the compiler binds it immediately. That is also why
@Overriderefuses them outright. - Private members stay home. They are not part of the inheritance contract, so same-named methods in parent and child are simply two unrelated methods.
Shadowing is legal, not wise. Tools warn about it, and interviewers ask about it; the fix is mechanical: rename the child's field, or read the parent's through a getter (super.name() is not a thing for fields, but a method is).
Name the look-alike yourself
Five pairs of members, five verdicts. Pick the label you think applies, and the lab explains what actually happens:
- If the pair is fields, the answer is shadowing; two slots exist.
- If the pair is static methods, the answer is hiding; no object, no dispatch.
- If one side is private, nothing is replaced, ever.
- If the parameters differ (even by a type), it is an overload, compile-time only.
- A near miss (like
equals(Book)instead ofequals(Object)) is an overload too, and the one your intent almost always wanted to avoid.
`@Override` is the checker behind all five cases. It turns every near miss into a compile error, which is why every intended override should carry it.
Look-alike lab
pick a pair, then name itParent
Child
Which one is it?
Choose a verdict above; the mechanism will explain itself.
Every scenario above was compiled and run with javac before this lab claimed anything.
The look-alike playbook
Keeping the traps out of real code, in order:
- Put
@Overrideon every method you intend to replace; near misses become errors immediately. - Never declare a field with the same name as a parent's field; rename it, or better, make the parent's field private and expose a method.
- When a static method seems to need polymorphic behaviour, that is nature saying it should be an instance method (or not live on the hierarchy at all).
- Private helpers are safe to reuse names with, because subclasses cannot collide with what they cannot see; but remember parent code keeps calling its own.
- Before any 'which method runs' question, ask the two-part test: does an object take part, and are the parameters identical?
Real overrides dispatch; everything else was decided before the program ran. When a look-alike surprises you, the fix is usually a rename or an added @Override, not a new feature.
Only a real override consults the object; look-alikes were decided at compile time
Same-name fields shadow (two slots, reference type picks), static methods hide (no object involved), private methods never inherit, and different parameters just overload. A true override needs inheritance, identical parameters, and no static, final or private on the parent side; then the object's class runs the body.
Interview angles
Why does `p.name` read the parent's field when the object is a Child?+
Fields are resolved by the compiler from the reference's declared type. Both fields exist in the object; the one you read depends on the type you read it through, not on the object's class.
Methods dispatch because behaviour is meant to vary; fields are plain slots, so the language keeps their access static and simple. Shadowing is legal but almost always accidental; rename the field or reach the parent's state through a method.
Parent p = new Child(); p.name; // "parent": reference type decides ((Child) p).name; // "child": cast, then the child slot
Common misconception: That the object's class decides for fields as well. It does not: there are two separate fields, and the reference type picks the slot.
Follow-up: How do you read the parent's field from child code, cleanly?
What happens when parent and child declare the same static method?+
The child's method hides the parent's. No object participates, so the compiler binds the call from the reference type, at compile time.
This is why utilities are static and why @Override refuses them. If you need the object to choose at runtime, the method must be an instance method; the class cannot have it both ways.
Parent ref = new Child(); ref.kind(); // parent's version: chosen from the reference type
Common misconception: That the same-signature static method overrides. It hides: the call you get depends on the type written in the source, which reads as a bug to everyone else.
Follow-up: How would you get the child's behaviour through a parent-typed reference?
Can a child method 'override' a private parent method?+
No. Private methods are not inherited, so there is nothing to override; the child's method is a brand-new method, and the parent's own code keeps calling the parent's.
Inside the parent class, calls to its private methods are resolved to that class permanently. The child can never intercept them, which is exactly why private is the safe home for internal helpers.
class P { private String tag() { return "P"; } String call() { return tag(); } } class C extends P { String tag() { return "C"; } } new C().call(); // "P": parent code calls its own private method new C().tag(); // "C": a new, unrelated method
Common misconception: That same signatures connect the two methods. Privacy severs the connection before signatures ever matter.
Follow-up: Where should a shared helper live if a subclass must customise it?
Why is `boolean equals(Book other)` a classic near miss?+
It has a different parameter type than equals(Object), so it is an overload. Collections call equals(Object), which still runs the identity version, and the new method is never used by them.
The signature that matters is equals(Object). A near miss compiles happily and looks correct in local code; @Override turns it into a compile error on the spot, which is why every intended override carries the annotation.
@Override public boolean equals(Object other) { ... } // the real contract public boolean equals(Book other) { ... } // near miss: an overload, used by nobody
Common misconception: That the 'better-looking' overload is what libraries call. Their calls are compiled against Object, and they match signatures exactly.
Follow-up: Which other well-known methods suffer the same near-miss trap?
What single question separates overrides from all the look-alikes?+
Does an object take part? A real override asks the object's class at runtime; every look-alike (fields, statics, privates, overloads) was fully decided by the compiler from the source types.
Fill in the pair: identical parameters and inherited access on the parent side are required for an override. If any row fails (parameters differ, static, final, private), the compiler has already picked the answer and no surprise should follow.
// same parameters + inherited + not static/final/private -> override // anything else -> decided at compile time
Common misconception: That the runtime is full of choices. The runtime chooses exactly one thing: which override of a real method runs on the actual object.
Follow-up: Which fixes turn each look-alike back into predictable code?