Command Palette

Search for a command to run...

Back to the lesson: Topic 5.2 — The super Keyword
Core Java · Example 2 of 4

The constructor trap, live

Child's field initializers run only after super() returns, so the first call sees default values.

super lets a subclass reach its parent: super(...) calls a parent constructor, super.method() runs the parent's version of an overridden method, and super.field reads a parent field that the child has hidden.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Create Employee(String name, double salary) with describe(), and Manager extends Employee with an extra teamSize. Manager's constructor must use super(...) and its describe() must reuse the parent's.

02

Write a Logger with log(String msg) that prints the message, and a TimestampLogger that overrides log to prefix [t=1], [t=2], … using a counter, then delegates to super.log.

03

Give a class three constructors chained with this(...) so that new Box() produces a 1x1x1 box and new Box(5) a 5x5x5 box. Print the volume of each.

Explain it without notes

01

What are the three uses of super, and when do you need each one?

02

Why must super(...) be the first statement of a constructor (before Java 25)?

03

Explain why a parent constructor that calls an overridable method can see null fields in the child.

04

How does a super.method() call differ from a normal method call at the bytecode level?

The constructor trap, live
Sign in to run this example in your browser.

Expected output

Base constructor sees: name=null, size=0
Child constructor sees: name=Child, size=42