super
Refers to the parent class: calls its constructor or its version of a method.
Available since: Java 1.0
Syntax
super(arguments); super.method();Examples
Calling the parent constructor:
class Animal {
String name;
Animal(String name) {
this.name = name;
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
}
void main() {
IO.println(new Dog("Rex").name);
}Output:
RexExtending the parent method instead of replacing it:
class Report {
String render() {
return "Report";
}
}
class SalesReport extends Report {
@Override
String render() {
return super.render() + ": sales";
}
}
void main() {
IO.println(new SalesReport().render());
}Output:
Report: salesNotes
If the parent has no constructor without parameters, the subclass must call super(...) explicitly — otherwise you get "constructor Animal in class Animal cannot be applied to given types". Before Java 22 super(...) had to be the first line of a constructor; since Java 25 you may validate arguments before it, but not use this.
See also
In the tutorial
- Inheritance — Class inheritance in Java: extends, protected, calling super(...), overriding methods and the @Override annotation.