Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A subclass declares a field protected String status; with the same name as the parent's field. Code that reads entity.status through a Parent-typed variable sees null, while methods in the subclass see the value. Explain and fix it.

Fields are never polymorphic. When the subclass declares its own protected String status, it hides the parent's field, so every Child object holds two separate status fields: code in Child reads and writes Child.status, while code that reads entity.status through a Parent-typed variable reads Parent.status, which was never set and is null. The fix is to delete the duplicate field and use the inherited one, or to access state through overridable methods; static analysis can flag the pattern before it ships.

Why this question is asked

This bug is confusing the first time we meet it, because the debugger shows the value right there in the object, and yet some code reads null. It often appears after a refactoring, when someone copies a field into a subclass to add an annotation or change its default. Interviewers ask it to check that we know which parts of Java are dispatched at run time and which are fixed at compile time, and whether we know how frameworks such as JPA and Jackson react to the same mistake. In this answer, we will reproduce the bug, explain why there are two fields, fix it in two ways, look at the framework side effects, and see how to catch it automatically.

1A Quick Recap: What Field Hiding Means

Instance methods are overridden: a call is dispatched at run time to the method of the object's actual class. Fields are different. A field access such as entity.status is resolved by the compiler from the declared type of entity, and the bytecode names that class's field directly. If a subclass declares a field with the same name as a field in its parent, it does not replace the parent field. It adds a second field and hides the first one from code that looks through the subclass type. This is called field hiding.

2Reproducing the Problem

Here is a reduced version of the bug. The parent has a status field, and someone added the same field to the subclass, perhaps to give it a default value.

Java
class Order {
protected String status;
String describe() { return "status=" + status; } // compiled against Order.status
}
class ExpressOrder extends Order {
protected String status; // hides Order.status
void ship() { status = "SHIPPED"; } // writes ExpressOrder.status
String statusInChild() { return status; } // reads ExpressOrder.status
}
ExpressOrder child = new ExpressOrder();
child.ship();
Order entity = child;
System.out.println(child.statusInChild()); // SHIPPED
System.out.println(entity.status); // null
System.out.println(entity.describe()); // status=null

The same object gives two answers. Code in ExpressOrder sees SHIPPED. Code that goes through an Order reference, including the inherited describe() method, sees null. A debugger shows both fields, often as status and Order.status, which is the clue that there are two.

3Why There Are Now Two Fields

When javac compiles entity.status, it looks at the declared type, Order, and emits a getfield instruction for Order.status. When it compiles status inside ExpressOrder, the nearest declaration is ExpressOrder.status, so it emits getfield or putfield for that one. At run time, the JVM simply reads the field named in the instruction. There is no lookup by the object's class, as there is for methods.

One ExpressOrder object in memory
FieldWritten byRead byValue
Order.statusnobodyentity.status, describe()null
ExpressOrder.statusship()statusInChild()SHIPPED
Both fields live in the same object; the declared type of the access decides which one we touch.

So the bug is not a race or a lost update. Each piece of code is consistently using its own field. They have simply never been the same field.

4Fix 1: Remove the Duplicate Field

The usual fix is to delete the field from the subclass and use the inherited one. If the duplicate was added to set a default value, we set the inherited field in the constructor instead.

Java
class ExpressOrder extends Order {
ExpressOrder() { status = "NEW"; } // initialises the inherited Order.status
void ship() { status = "SHIPPED"; }
}
Order entity = new ExpressOrder();
((ExpressOrder) entity).ship();
System.out.println(entity.status); // SHIPPED
System.out.println(entity.describe()); // status=SHIPPED

Now there is one field, and every piece of code agrees on its value.

5Fix 2: Access State Through Methods

A more robust design keeps fields private and exposes them through methods. Methods are polymorphic, so a subclass that needs different behaviour overrides the method rather than shadowing data.

Java
class Order {
private String status = "NEW";
public String status() { return status; }
protected void changeStatus(String s) { status = s; }
}
class ExpressOrder extends Order {
void ship() { changeStatus("SHIPPED"); }
@Override public String status() { return "EXPRESS-" + super.status(); } // dispatched at run time
}
Order entity = new ExpressOrder();
((ExpressOrder) entity).ship();
System.out.println(entity.status()); // EXPRESS-SHIPPED

Because the field is private, a subclass cannot even see it, so it cannot accidentally hide it in a confusing way. Any variation goes through methods, where the JVM chooses the right version from the object.

6Why Frameworks Get Confused Too

Frameworks that map fields by name hit the same ambiguity, and their results are often worse than null. With JPA, if a mapped superclass and an entity both declare a field called status, the provider sees two persistent attributes with the same name. Depending on the provider and version, this fails at startup with a duplicate column or attribute error, or one of the two fields is quietly ignored, so values are not saved or loaded where we expect.

Jackson builds its property list by walking fields and getters across the class hierarchy. Two fields with the same name can lead to a conflicting property definitions error, or to the wrong one of the two being serialized, depending on visibility settings and annotations. In both cases the root cause is the same: two fields exist where we meant one. Check your framework version for the exact behaviour, but the fix is always to remove the duplicate.

7Catching It With Static Analysis

Field hiding is easy to detect mechanically, so we should not rely on code review. Error Prone has a HidingField check that reports a field hiding a superclass field, Sonar has a rule against child class fields shadowing parent fields, and IntelliJ IDEA has a matching inspection. Turning one of these on as a build error in the shared configuration prevents the bug from coming back.

Java
// Error Prone, in the compiler arguments
// -Xplugin:ErrorProne -Xep:HidingField:ERROR
// ExpressOrder.java:2: error: [HidingField] Hiding fields of superclasses may cause confusion and errors.

8Follow-up questions

Is field hiding ever useful?

Very rarely. It can appear legitimately with static constants, for example a subclass declaring its own static final VERSION. Even then, readers can be misled about which value a reference uses. For instance fields, there is practically no good reason, and private fields with accessor methods avoid it completely.

How do you read the hidden parent field from the subclass?

Inside the subclass, super.status refers to the parent's field, and ((Order) this).status does the same. Needing either is a strong sign that the design should have a single field.

Why did the debugger show the value while the code read null?

Debuggers list every field in the object, including hidden ones, so we see the subclass field holding SHIPPED. Code compiled against the parent type reads the other field, which the debugger also shows, often labelled with the parent class name. Looking for two entries with the same name is the quickest way to spot this bug.

9Conclusion

In this answer, we saw that fields are never polymorphic: a subclass field with the same name hides the parent field, so each object holds two separate fields and the declared type of each access decides which one is read. That is why a Parent-typed read saw null while subclass methods saw the value. We fixed it by removing the duplicate field or by keeping fields private behind overridable methods, saw how JPA and Jackson get confused by the same duplication, and caught it automatically with Error Prone's HidingField check or the equivalent Sonar rule.

What to say in the interviewAbout 90 seconds aloud

Open with

The key point is that fields are never polymorphic. When the subclass declared protected String status with the same name as the parent's, it didn't override anything; it added a second field that hides the first.

Then, if they want more

So every subclass object now holds two status fields. javac resolves a field access from the declared type, so code inside the subclass compiles to putfield and getfield on the subclass's status, while code that reads entity.status through a Parent-typed variable, including inherited methods in the parent, compiles to a read of Parent.status.

Nobody ever wrote that one, so it's null. The debugger usually shows both fields, often one labelled with the parent class name, which is the giveaway. The fix is to delete the duplicate field and use the inherited one, setting any default in the constructor.

Better still, keep the field private and expose it through methods, because methods are dispatched at run time, so if a subclass needs different behaviour it overrides the getter instead of shadowing data.

I'd also mention that frameworks that map by field name, like JPA and Jackson, get confused by the duplicate, causing startup errors or values that are silently not saved or serialized. To stop it recurring, I'd enable Error Prone's HidingField check or the Sonar rule against shadowing parent fields as a build error.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A subclass declares a field protected String status; with the same name as the parent's...": https://scrivio-interview-library.netlify.app/java/oop-principles/e5b35caffe/