Concepts / Hiding Superclass Members in Java

Hiding Superclass Members in Java

super is a special variable used inside a subclass to refer to the object's superclass view, and it remembers only that the object belongs to the superclass of the class being written.

  • Programming

Why super Is Not a Mirror

Many learners meet the word super for the first time and assume it works like a mirror: a reference that sees the whole object, including everything the subclass has added or changed. The belief feels reasonable. The object really does contain both the subclass pieces and the superclass pieces. This article answers a narrower question. When Java code inside a subclass writes super, what is it allowed to see, and what is it blind to?

What do you think happens?

Vehicle has a method start(). Truck extends Vehicle and adds its own method haul(). Inside Truck, a programmer writes super.haul(). Should this call work?

  • Yes, because the object contains the Vehicle part and the Truck part

  • No, because super cannot reach methods the subclass added

Reveal answer
Answer:

No. super.haul() does not compile.

super sees the object only as a member of the superclass. Adding haul() in Truck is an addition the subclass made to its superclass, and super does not know about additions or modifications. Only methods and variables defined in the superclass are reachable through super.

What the Word super Refers To

Definition

super is a special variable used inside a subclass to refer to the object's superclass view. It remembers only that the object belongs to the superclass of the class being written.

The source describes super as a view onto the object, not a view onto the whole object. Inside a class named Truck that extends Vehicle, the object is seen as a Vehicle and nothing more specific than that. The phrase nothing more specific is the key. Truck-specific members are part of the same object, but they are outside what super is able to see.

If super can see only the superclass, why does it exist at all? The source gives the purpose directly: super exists so you can reach things in the superclass that have been hidden by things in the subclass. Hiding happens when a subclass declares something that shares a name with a superclass member. Without super, the superclass version would be out of reach from subclass code, because the subclass's own name would always win.

One Object, Two Variables

Consider what an object actually holds when a subclass declares an instance variable with the same name as one in its superclass. The source says the object contains two variables with the same name: one defined as part of the subclass and one defined as part of the superclass. The subclass variable does not replace the superclass variable. It hides it. The superclass variable still exists and can still be accessed, using super.

Vehicle part namevalue vehiclesuper.namereaches Vehicle partTruck part namevalue trucknamereaches Truck partrefers torefers to
When Truck hides the name field of Vehicle, how many variables exist in one object, and which one does super.name reach versus plain name?

The diagram shows the state after the object is created. Both variables exist at once. Inside Truck, the plain name points at the subclass variable, while super.name always points at the superclass variable. The meaning of super.name does not depend on what the subclass did with name. It always refers to the superclass version.

Tracing a Hidden Variable

Worked example

Reading both name fields from inside Truck

Vehicle declares a String field name with the value vehicle. Truck extends Vehicle and declares its own String field name with the value truck. A method inside Truck prints both name and super.name. What prints?

  1. 1
    Identify the two variables

    The object has a Vehicle-part name and a Truck-part name. Both exist even though they share a name.

  2. 2
    Resolve the plain name

    Inside Truck, plain name refers to the subclass variable, which holds truck.

  3. 3
    Resolve super.name

    super.name always refers to the superclass variable, which holds vehicle.

  4. 4
    Combine the results

    The first print shows truck and the second print shows vehicle.

Result

The output is truck on the first line and vehicle on the second line.

java
Output
truck
vehicle
Notes on the code
  • Truck declares name, which hides the inherited Vehicle field of the same name.
  • Plain name inside showNames refers to the Truck field.
  • super.name refers to the Vehicle field, even though Truck hides it.

Calling an Overridden Method Through super

The same pattern applies to methods. When a subclass contains an instance method with the same signature as a method in its superclass, the superclass method is hidden in the same way, and the subclass method overrides it. A signature here means the method's name and parameter list. Calling the method on a subclass object runs the subclass version.

Overriding does not remove the superclass method. As with variables, super can still reach it. A subclass method can call super.start() to run the superclass version as part of its own work. The sequence below traces that control flow.

Callermain methodTruck start()overriding versionVehicle start()superclass version1. start() on Truck object2. super.start()3. returns4. returns
When a subclass method calls super.start(), what is the sequence of calls and which version of the method runs at each step?
Output (expected)
Vehicle starting
Truck engaging load
Notes on the code
  • Truck overrides start, so calling start on a Truck object runs the Truck version.
  • super.start() runs the Vehicle version as part of the Truck method.
  • The Truck method finishes its own work after the superclass call returns.

The Boundary Between super and the Subclass

A useful way to check any member access inside a subclass is to ask which side of the boundary it sits on. Members the superclass defined are reachable through super. Members the subclass added on its own are reachable only through the bare name or through this, never through super.

super.nameVehicle fieldhaul()added by Trucksuper.start()Vehicle methodcargofield added by Truck
Which members are reachable through super, and which exist only on the subclass side of the boundary?

The left column is the superclass view that super exposes. The right column is what the subclass defined itself. Hiding and overriding are the cases where a name appears on both sides, which is exactly why super is needed to reach the left-hand version.

Field Hiding Versus Method Overriding Through super

SituationWhat super refers toWhat the subclass does
Subclass field with the same name as a superclass fieldThe superclass field, always, through super.varDeclares a second variable; the two coexist in one object and the subclass one hides the other
Subclass method with the same signature as a superclass methodThe superclass method, through super.method()Overrides the method; calling the method on a subclass object runs the subclass version
Subclass-only field or methodNothing; super cannot reach itExists only on the subclass side and is reached by the bare name or this

The three cases a super reference can encounter inside a subclass.

Mistakes Learners Make With super

  • Common mistake

    Expecting super to see methods the subclass added

    What it looks like

    Inside Truck, writing super.haul() when haul() is defined only in Truck.

    Why it fails

    super is limited to the superclass view. Additions the subclass made are invisible through super.

    Do this instead

    Call haul() with the bare name or through this, since it belongs to the subclass side.

  • Common mistake

    Thinking a hidden field has been removed from the object

    What it looks like

    Assuming that after Truck declares name, the Vehicle name no longer exists.

    Why it fails

    The source says the object contains two variables with the same name. The subclass variable hides the superclass one but does not replace it.

    Do this instead

    Use super.name to reach the superclass variable when you need it.

  • Common mistake

    Assuming an overridden method is gone

    What it looks like

    Writing a Truck start() method and believing the Vehicle start() can no longer run.

    Why it fails

    Overriding does not remove the superclass method. super can still call it.

    Do this instead

    Use super.start() inside the overriding method to run the superclass version.

  • Common mistake

    Confusing this with super when both could seem to work for inherited members

Practice: Predicting What Prints

EASY

Vehicle has a field named speed with the value 40. Truck extends Vehicle and declares a field named speed with the value 90. Inside a Truck method, what do the expressions speed and super.speed produce? Then explain what changes if Truck removes its speed field.

Hints
  • Ask whether the object holds one variable or two when a subclass declares a field with the same name.
  • super.speed always refers to the superclass version, regardless of what the subclass does with speed.
  • If Truck removes its own speed field, the plain name no longer has a subclass version to hide the inherited one.

Key Takeaways

  • super is a view of the object as its superclass. It remembers only that the object belongs to the superclass of the class being written, so it cannot see members the subclass added or changed beyond the superclass.
  • A subclass variable with the same name as a superclass variable hides it rather than replacing it. The object holds both, and super.var always reaches the superclass version.
  • A subclass method with the same signature overrides the superclass method, but super can still call the overridden version, for example to run superclass code as part of the subclass method.
  • When reading or writing a member inside a subclass, check whether it belongs to the superclass side (reachable through super) or was defined only by the subclass (reachable only through the bare name or this).