Reference: super
When we override superclass's methods, but still want to invoke them, we can use keyword super in child classes. We can also use super
to refer to a hidden field (although hiding fields is discouraged).
// Superclass is parent class
public class Superclass { public void printMethod() {
System.out.println("Printed in Superclass.");
}
} // Subclass is child class
public class Subclass extends Superclass { // overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}
Compiling and executing Subclass
prints the following:
Printed in Superclass.
Printed in Subclass
We can also use super() or super(parameter list) to invoke superclass's constructor.