← Back to Java

super Keyword

JavaEntryjava

The Question

What is the purpose of the super keyword in Java?

What a Strong Answer Covers

  • refers to immediate parent class
  • access hidden parent fields
  • call overridden parent methods
  • call parent constructor must be first statement
  • three uses

Senior-Level Answer

The super keyword in Java is a reference to the parent (superclass) of the current class. It has three primary uses: calling a parent class constructor, calling an overridden parent class method, and accessing a parent class field hidden by a subclass field of the same name.

Calling a parent constructor: super() or super(args) must be the first statement in a subclass constructor. If omitted, the compiler inserts an implicit super() call that invokes the parent's no-arg constructor. If the parent class does not have a no-arg constructor (because it defines only parameterized constructors), the subclass must explicitly call super(args) — otherwise the code will not compile.

Calling an overridden parent method: when a subclass overrides a method, the parent's version is still accessible as super.methodName(). This is useful when you want to extend, not replace, the parent's behavior. For example, a subclass's toString() might return super.toString() + ", additionalField=" + field, building on the parent's representation.

Accessing hidden fields: if a subclass declares a field with the same name as a parent field, the parent's field is shadowed. super.fieldName accesses the parent's field. This pattern is rare and generally a code smell — shadowing fields leads to confusing behavior.

super cannot be chained (super.super.method() is not valid in Java). This is deliberate: deep chains would couple subclasses to grandparent implementation details, which is considered harmful to encapsulation.

A key distinction: super() is for constructor chaining up the hierarchy; this() is for constructor chaining within the same class. They cannot both be the first statement, so you must choose one or the other in a given constructor.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Covers the constructor call use (super() as first statement) and the overridden method call use (super.method()), with correct syntax.

3/3 — Strong Answer

Adds the implicit super() insertion rule, the field hiding use case (plus the code smell note), the super.super limitation and rationale, and the contrast with this() constructor chaining.

Common Mistakes

  • Thinking super() can appear anywhere in the constructor — it must be the first statement or the compiler rejects it.
  • Not knowing that the compiler auto-inserts super() when omitted — this causes subtle compilation errors when the parent lacks a no-arg constructor.
  • Attempting super.super.method() — this is not valid Java syntax and demonstrates a misunderstanding of how super works.

Follow-Up Questions

  • What happens when a parent class does not have a no-arg constructor and the subclass constructor does not call super(args)? — The compiler inserts super() by default, finds no matching no-arg constructor in the parent, and emits a compile error — the subclass must explicitly call super(args).
  • Can you call super() and this() in the same constructor? — No — both must be the first statement, so only one can appear. You must choose constructor chaining within the class (this()) or up to the parent (super()).
  • When is it appropriate to call super.method() in an overriding method? — When the override extends rather than replaces behavior — common in framework callbacks (onCreate(), toString()), where the parent implementation must still run and the subclass adds to it.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout