ReviseAlgo Logo

Inheritance

super Keyword

Deconstruct parent reference access, field shadowing resolutions, constructor invocation rules, and static context constraints.

Interview: Evaluates parent constructor invocation sequencing, field shadowing resolution with super, and compiler rules regarding super in static contexts.

Last Updated: June 13, 2026 10 min read

The super keyword in Java is a reference variable used to refer directly to the immediate parent class. It serves three main purposes: invoking parent constructors, accessing parent fields (resolving shadowing), and calling overridden parent methods.

Constructor Invocation

Used as super() or super(args) to call the parent constructor. It must be the absolute first statement in a subclass constructor.

Field Shadowing Resolution

If a subclass defines a field with the exact same name as a parent field, super.fieldName bypasses the local subclass field to access the parent's field.

Static Scoping Rules

Because super references an instance context, it cannot be used within static methods, static variables, or static initialization blocks.

Field Shadowing and Method Overriding Resolution

When a subclass defines a field or method that hides or overrides a parent member:

  • Field Shadowing: Unlike methods, fields are not polymorphic. If both parent and child declare field int count, the child object allocates two distinct slots. this.count targets the child's field, while super.count targets the parent's field.
  • Overridden Methods: If the child overrides void print(), calling super.print() explicitly invokes the parent class implementation, bypassing runtime dynamic method dispatch.

Static Context Restrictions

The Java compiler strictly prohibits using super inside static contexts. A static method belongs to the class itself, loaded in the Metaspace. Because it is executed without any reference to a heap-allocated instance, the keywords super and this (which rely on the active object's stack frame reference pointer) have no meaning and yield a compile-time error.

Common Pitfalls

  • Placing super() after other logic: Attempting to place statements (like field initialization) before calling super(...) inside a constructor, resulting in compiler failure.
  • Shadowing fields accidentally: Creating subclass variables with names identical to parent variables, leading to hard-to-track bugs where subclass methods operate on shadowed state instead of parent state.

Best Practices

  • Avoid Field Shadowing: Always name subclass fields uniquely. If you need to modify parent state, use protected/public setter methods.
  • Leverage super for Method Decoration: Use super.methodName() to wrap and extend parent method behaviors rather than completely replacing them.

Interview-Relevant Information

Q1: Can we use both this() and super() inside the same constructor?
Answer: No. Both this() (calling another constructor of the same class) and super() (calling a parent constructor) must be the first statement in a constructor. Since you can only have one first statement, they cannot be used together.

Q2: Why must super() be the first statement in a constructor?
Answer: Java enforces top-down object construction to guarantee that the parent class state is fully initialized before the subclass initializes its own state. This prevents subclasses from accessing uninitialized parent members during execution.

Quick Checklist

Can you write a subclass constructor that chains to a parameterized parent constructor, resolve field shadowing conflicts with super, and explain why super is banned from static methods? If yes, you have mastered the super keyword.

Use Cases

Chaining parameters through complex subclass hierarchies in customized class initializers.

Wrapping standard logging, security, or validation checks around inherited method behaviors.

Common Mistakes

Writing operations before super() inside subclass constructors, yielding compile errors.

Expecting field names to act polymorphically when shadowed, resulting in bugs where child methods mutate the wrong variable.