Subclass Field Shadows Superclass Field

ID

java.child_class_fields_shadow_parent

Severity

critical

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

code-style, reliability

Description

Reports a field declared in a subclass whose name matches, case-insensitively, a visible field of its superclass.

Rationale

Java allows a subclass to declare a field with the same name as one inherited from its superclass; the two fields then coexist independently, and which one a given piece of code reads or writes depends only on the static type of the reference used at that point. Nothing about the syntax warns a reader that two different storage locations share a name, which makes the class easy to misuse by accident.

A superclass field that is private is not visible from the subclass at all, so a same-named subclass field is not shadowing anything — it is simply unrelated. Two same-named fields that are both static are also left alone, since a shadowed static field is a narrower, deliberate pattern rather than the accidental case this rule targets.

// Bad - Cart.total shadows Order.total; assigning one leaves the other untouched
public class Order {
    protected double total;
}

public class Cart extends Order {
    private double total;
}

Remediation

Rename the subclass field, or reuse the inherited one instead of redeclaring it.

// Good
public class Order {
    protected double total;
}

public class Cart extends Order {
    private double discountedTotal;
}

© 2026 Xygeni. All rights reserved