Java Notes
Learn the super keyword in Java — how to access a parent class field, call a parent method after overriding, and call a parent constructor using super().
When a child class inherits from a parent, sometimes the child needs to reach back into the parent — to call its constructor, use a field it shadowed, or run the original version of a method it overrode. The super keyword does exactly that.
super always refers to the parent class of the current class. It has three uses.
Use 1: Calling the Parent Constructor — super()
When you create a child object, the parent's constructor must run to set up the inherited part of the object. super(...) calls it explicitly.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
super(name, "Woof")— must be the first statement in Dog's constructor. It passesnameand the fixed sound"Woof"to Animal's constructor.- Animal's constructor runs first — sets
this.name = "Bruno"andthis.sound = "Woof". - Then Dog's constructor continues — sets
this.breed = "Labrador". d.introduce()usesnameandsound(from Animal) andbreed(from Dog).
If you do not write super(...), Java automatically inserts super() (no arguments). If the parent has no no-argument constructor, you get a compile error. This is why you must explicitly write super(name, "Woof") here.
Use 2: Accessing a Parent Field That the Child Shadowed
If a child declares a field with the same name as a parent field, it hides the parent's version. super.fieldName reaches the parent's version.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
type— refers to Car's owntypefield ("Car").super.type— refers to Vehicle'stypefield ("Generic Vehicle").
Field hiding is uncommon in real code. Most of the time you will use super to access parent methods, not fields.
Use 3: Calling the Parent Version of an Overridden Method
When a child overrides a method, calling the method uses the child's version. super.methodName() runs the parent's original version.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
d.describe()— calls Dog's overriding version.super.describe()inside Dog — runs Animal's originaldescribe().- Output: first Animal's line, then Dog's line.
Without super.describe(), Animal's version would be completely replaced — it would never print.
Multilevel Chain: super in Three Levels
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Order: A → B → C. The deepest ancestor always initialises first. Every super() call climbs one level up the chain.
super vs this
this | super | |
|---|---|---|
| Refers to | Current object | Parent class |
| Constructor call | this() — another constructor in same class | super() — parent's constructor |
| Field access | this.field — current class's field | super.field — parent's field |
| Method call | this.method() — current class's version | super.method() — parent's version |
| First statement rule | this() must be first in constructor | super() must be first in constructor |
| Can be used together? | No — this() and super() cannot both be first |
What super Cannot Do
super does not work in static methods — the same reason this does not work there.
public static void test() {
super.someMethod(); // COMPILE ERROR — no super in static context
}Common Mistake 1: Putting super() After Another Statement
// WRONG
public Dog(String name, String breed) {
this.breed = breed; // some statement first
super(name, "Woof"); // COMPILE ERROR — super() must be first
}
// CORRECT
public Dog(String name, String breed) {
super(name, "Woof"); // first
this.breed = breed;
}Common Mistake 2: Expecting super to Skip Levels
// In class C (which extends B which extends A):
super.method(); // calls B's version, NOT A's version directlysuper always goes one level up — to the immediate parent. It cannot jump two levels.
Practice Task
Task: Create a class Employee with fields name and baseSalary, and a method getSalaryInfo() that prints "Base salary: [baseSalary]". Create a child class Manager that adds a field bonus and overrides getSalaryInfo() to first call the parent version then print "Bonus: [bonus]" and "Total: [baseSalary + bonus]". Use a proper constructor with super(name, baseSalary).
Hint: Inside Manager's getSalaryInfo(), use super.getSalaryInfo() to print the base line, then access baseSalary (inherited field) and bonus for the total.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: When m.getSalaryInfo() runs, it first calls Employee's version (via super), then adds its own lines. Remove super.getSalaryInfo() — Employee's line disappears. The parent logic is gone.
Continue the lesson path