Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

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:

Rex

Extending 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: sales

Notes

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

  • extends — Declares inheritance: the class gets the parent's fields and methods.
  • this — A reference to the current object — the one whose method or constructor is running.
  • abstract — Marks a class that cannot be instantiated and a method without a body that subclasses implement.

In the tutorial

  • Inheritance — Class inheritance in Java: extends, protected, calling super(...), overriding methods and the @Override annotation.

Thanks for reading!

Donate to the site — we work hard for you.