Interview Q&A · Java · 4 questions in this topic

Field Hiding — Java Interview Q&A

All Java topics

Why a subclass field with the same name hides rather than overrides its parent field, and how that differs from method overriding. Part of Interfaces, Enums & Features · 294 questions in the Java bank.

Field hiding is a compile-time lookup rule, not polymorphism. When a subclass declares a field with the same name as a superclass field, both fields exist in the object; the reference type of the expression decides which one is read. Methods behave differently because they are dispatched on the object’s runtime type. This is why shadowing a field is usually a bug: the subclass value silently fails to affect inherited methods that read the superclass field. Keep fields private and expose behaviour through methods to avoid the trap.

What is field hiding?

Answer

Field hiding occurs when a subclass declares a field with the same name as a field in its superclass. Unlike methods, fields are never overridden: the subclass field merely hides the superclass one, and which field is read is fixed at compile time from the reference type. Methods still dispatch dynamically on the real object type, so a hidden field and an overridden method behave differently through the same reference.

Key points

  1. 1Reading super.label inside the subclass, or casting the reference to Parent, still returns the parent value.
  2. 2Hiding a local variable or parameter is a different compile-time scoping rule, not the same as field hiding.
  3. 3Prefer distinct names or a getter; depending on the reference type makes the code fragile to refactoring.

Example

Example.javajava
class Parent {
    String label = "parent";
    String get() {
        return "parent method";
    }
}
class Child extends Parent {
    String label = "child";
    String get() {
        return "child method";
    }
}
Parent p = new Child();
System.out.println(p.label); // parent: field follows the reference type
System.out.println(p.get());   // child method: methods follow the object type

💡 Expect a follow-up contrasting hiding with overriding: fields bind to the reference type while methods bind to the object type.

What is field hiding, and how does it differ from shadowing?

Answer

Field hiding happens between classes: a subclass that declares a field with the same name as a superclass field hides it, and both fields still exist in every instance. Which one you read depends on the declared type of the reference, not on the object’s runtime type — a classic source of confusion. Shadowing is the local case: a parameter or local variable with the same name as a field shadows it inside that scope, which is why constructors write this.name = name.

Key points

  1. 1Hiding is resolved at compile time from the reference type.
  2. 2Shadowing is a local variable or parameter covering a field.
  3. 3this.field reaches the field when a parameter shadows it.
  4. 4Hiding fields is legal but almost always a design smell.

Example

Example.javajava
class A { String name = "A"; }
class B extends A { String name = "B"; }
A ref = new B();
ref.name;   // "A" — the field is chosen by reference type

Can static methods be overridden?

Answer

No — static methods are hidden, not overridden. There is no dynamic dispatch: the call is resolved from the type of the reference at compile time, so a variable declared as the superclass runs the superclass version even when it points at a subclass instance. Overriding requires an instance method and a runtime object. Declaring an instance method in a subclass with the same signature as a static superclass method is a compile error for exactly that reason.

Key points

  1. 1Static methods are chosen by reference type, not object type.
  2. 2Hiding a static method is legal; overriding it is not.
  3. 3Changing static to instance (or back) in a subclass does not compile.
  4. 4Calling a static method through an instance is legal but misleading.

Learn this topic

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits