Java super
Use super inside a child constructor so the parent constructor runs first. Keep Truck extends Car. In Truck(), call super();, then print truck. In Car(), print car. Compile Yard.java and run java Yard. The screen should show car, then truck. A second demo passes a mark with super("car") into Car(String mark). Leave overriding a method for the next page — this lesson is about constructor order with super.
Call the parent constructor first
The inheritance page let an empty Truck use horn from Car. This page adds constructors. Open Yard.java. Keep the public class name Yard. Delete leftover path demos, leftover horn-only files if they confuse the constructors, leftover Store writers, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Declare the parent with a no-argument constructor that prints car:
class Car {
Car() {
System.out.println("car");
}
}
Declare the child with extends Car. Inside Truck(), call super(); as the first statement, then print truck:
super();
System.out.println("truck");
Inside Yard.main, build one truck:
Truck t = new Truck();
Building the truck runs Truck(). That call starts with super(), which runs Car(). After the parent finishes, the child print runs.
class Car {
Car() {
System.out.println("car");
}
}
class Truck extends Car {
Truck() {
super();
System.out.println("truck");
}
}
public class Yard {
public static void main(String[] args) {
Truck t = new Truck();
}
}
From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard. Helper types Car and Truck stay without public on this beginner shape.
The screen shows car, then truck. Walk the run once. Java starts at Yard.main. new Truck() enters Truck(). The first statement is super();, so Car() runs and prints car. Control returns to Truck(), which prints truck. The parent line appears before the child line because super() ran first.
Say the order out loud while you watch the screen: parent constructor, then child print. That spoken order matches what super() forces in the child constructor. If the lines arrive in the wrong sequence, open the teaching file again and check that super(); is still the first statement inside Truck() before any println.
If you print truck before super() in a later experiment where the parent constructor needs arguments, javac can refuse — super must be the first statement when you write it. On this first demo the parent needs no arguments, and the teaching file keeps super(); first on purpose so the order stays clear. If you see only beep from an old horn demo, switch to the constructor teaching file and run java Yard again.
Do not rewrite horn on Truck on this page. Overriding a method is the next lesson. Today the child constructor talks to the parent with super, then prints its own word. Keep the word car on the parent side and the word truck on the child side so each line tells you which constructor finished.
Pass a value into the parent with super
A second program keeps extends. The parent constructor now takes a String mark and prints that mark. The child calls super("car") and then prints truck.
class Car {
Car(String mark) {
System.out.println(mark);
}
}
class Truck extends Car {
Truck() {
super("car");
System.out.println("truck");
}
}
public class Yard {
public static void main(String[] args) {
Truck t = new Truck();
}
}
Compile with javac Yard.java, then run java Yard. The screen again shows car, then truck. This time car is the argument you passed through super("car"), not a hard-coded print inside a no-argument Car(). The child chose the mark string; the parent constructor printed it. After that, the child printed truck.
Read the parent constructor signature once more: Car(String mark). The string in quotes inside super("car") is that mark. Change nothing else in the file for this check — keep extends Car, keep the child print of truck, and keep new Truck() in main. The screen still ends with truck under car because the child print runs only after super("car") returns.
If you omit super("car") while Car only has Car(String mark), javac refuses, because the parent no longer has a silent no-argument constructor to call for you. Writing the matching super(...) call is how the child starts the parent with the values it needs. Keep super as the first statement in Truck(). If javac complains about a missing default constructor on Car, look at the child constructor first and confirm the super("car") line is present and still first.
Compared with plain extends on the horn page
On the inheritance page, an empty Truck called horn without writing constructors. On this page, constructors run in order through super. Keep the horn demos as history if they still sit nearby; run the super teaching file with java Yard so the screen shows car then truck.
The horn page proved that extends shares a method. This page proves that extends also shares a construction path: the child starts the parent through super, then continues with its own print. Both lessons keep Truck extends Car. Only the body of Truck changes — from empty braces with a later horn call, to a constructor that begins with super.
super(), super("car"), and constructor order are the new tokens. You still need extends. You do not need to override a method yet. If a leftover horn call still sits in main, remove it for these demos so the screen only shows the constructor prints.
What this leaves for overriding
This page runs parent constructors with super. The next page shows overriding, where a child writes its own version of a method the parent already has. Stay with constructor super calls here. Do not replace horn on Truck as a requirement on these demos yet.
When you later override a method, you will still often call a parent constructor with super first. Treat this page as the constructor half of that story. Finish both Yard demos until car then truck feel automatic before you open the overriding lesson.
Before overriding
You can call super(); then print truck and see car first. You can call super("car") when the parent takes a mark and still see car, then truck. Compile Yard.java and run java Yard for each kept demo in turn. The parent constructor finished before the child print. The next page is overriding, where the child replaces a method body.
Keep the no-argument super() demo for a clear order. Keep the super("car") demo for a clear argument pass. Keep method override off this page. Those three habits are the checkpoint before you move on. When car appears above truck after new Truck(), super did its job for this lesson and you are ready for the next topic about overriding.
One last pass through the folder: one public Yard, two helper types, no keyboard wait, no path leftovers, no forced horn rewrite. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still prints car then truck, the constructor order is solid and the course can move to overriding on the next page.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top