The super Keyword in Java - Quiz

Total: 6 questions

1. 

What is the super keyword in Java and what forms does it have?

super is a reference to the immediate superclass (parent class) of the current object — to the part of the object that came from the parent. It comes in exactly two forms:

1. A superclass constructor invocationsuper(argumentList);. Allowed only inside a constructor, and only once.

2. Access to a superclass membersuper.member: a field hidden by a same-named field of the subclass, or a method overridden in the subclass.

Note that super is neither an object nor a variable: Object o = super; does not compile. It is a special access form that the compiler turns into a non-virtual call (the invokespecial bytecode instruction). Inside a static method or a static initializer super is unavailable, because there is no instance yet.

2. 

Why does code stop compiling with there is no default constructor available, and who inserts the implicit super()?

When a subclass constructor contains no explicit super(...) or this(...) call, the compiler — not the JVM — inserts super() as the first statement:

public HeavyBox() {
    super();          // inserted by the compiler
    this.weight = -1;
}

As soon as the superclass declares at least one constructor of its own, the default constructor is no longer generated. The implicit super() then has nothing to bind to, and the build fails with there is no default constructor available in Box — in subclass files you never edited.

Two ways to fix it: add a no-argument constructor to the parent, or call an existing one explicitly, for example super(width, height, depth);.

3. 

In what order do constructors run when an object of a multi-level class hierarchy is created?

Constructor bodies run from the root of the hierarchy down to the current class. For the hierarchy SuperSuperClassSuperClassSomeClass, the statement new SomeClass() prints:

In the SuperSuperClass constructor
In the SuperClass constructor
In the SomeClass constructor

The mechanics: every constructor first passes control upward through the chain of explicit or implicit super() calls, all the way to java.lang.Object, and the bodies execute on the way back down, from ancestor to descendant. The reason is practical — by the time a subclass constructor body starts, the inherited part of the object is already fully initialized.

Expecting the opposite order in the logs is a classic interview trap.

4. 

What is super.member for, and why does super.print() inside an overridden print() not recurse forever?

super.member is needed only when the name is shadowed in the subclass; otherwise inherited members are already visible by their plain names.

public class D extends C {
    public String i;          // hides int i declared in C

    public D(String a, int b) {
        i = a;
        super.i = b;          // the only way to reach int i
    }

    public void print() {
        System.out.println("D.i = " + i);
        super.print();        // runs the implementation of C
    }
}

Fields are hidden and resolved by the static type of the reference, while methods are overridden and resolved by the actual type of the object. A plain print() call inside D.print() would dispatch back to D.print() and recurse forever. super.print() compiles into a non-virtual call (invokespecial) bound to the superclass implementation, so no recursion happens.

The form super.super.method() is forbidden in Java: you cannot skip a level of the hierarchy. To call an interface default method use the qualified form InterfaceName.super.method(), for example Walkable.super.move().

5. 

How do super(...), this(...), super.member and this.member differ? Can one constructor call both super() and this()?

super(...) invokes a constructor of the superclass, this(...) invokes another constructor of the same class. Both are allowed only inside a constructor, and only once.

super.member reaches a hidden field or an overridden method of the parent, while this.member reaches a field or method of the current object. These two forms work in any instance method and in a constructor.

No — super(...) and this(...) are mutually exclusive: a constructor may contain exactly one explicit invocation. When it starts with this(...), the compiler adds no implicit super(); the superclass constructor runs inside the constructor you delegated to. Either way, exactly one super(...) call happens per created object.

6. 

What are flexible constructor bodies, since which Java version can you write statements before super(), and what is forbidden in the constructor prologue?

Historically super() or this() had to be the first statement of a constructor. The restriction was lifted step by step: JEP 447 (Java 22, preview) → JEP 482 (Java 23) → JEP 492 (Java 24) → JEP 513 (Java 25), a final feature that needs no --enable-preview flag. On the LTS releases Java 17 and Java 21 the classic rule still applies.

A constructor body splits into a prologue (everything before super() or this()) and an epilogue (everything after it). The governing rule of the prologue: the object under construction cannot be touched yet. In the prologue you may assign to fields of the current class, call static methods and run any local computation, including checks, loops and throw. You may not read fields, touch inherited fields, call instance methods or pass this anywhere.

public HeavyBox(int width, int height, int depth, int weight) {
    if (width <= 0) {
        throw new IllegalArgumentException("width must be positive");
    }
    super(width, height, depth);
    this.weight = weight;
}

Targeting Java 17 or 21? Move the argument preparation into a private static method and call it right inside super(...).

Page 1 of 1