What are the uses of the super keyword?
super refers to the parent part of the current object. It calls the parent constructor, calls a parent method that the child has overridden, or reaches a parent field hidden by a child field.
Explain it simply
A child can say "my own style" or "as my parent does it". this points to the child's version. super points to the parent's version, which is useful when you want to extend behaviour rather than replace it.
Example
class Employee {
String name;
Employee(String name) { this.name = name; }
double bonus() { return 5000; }
}
class Manager extends Employee {
Manager(String name) {
super(name); // parent constructor, must be first line
}
@Override
double bonus() {
return super.bonus() + 10000; // parent's logic plus extra
}
}
System.out.println(new Manager("Kavya").bonus()); // prints: 15000.0super(...) and this(...) cannot both appear in one constructor, because each must be the first statement.
What to say in the interview
"super gives access to the parent class's members. I use super(...) to run the parent constructor, super.method() to reuse and extend overridden behaviour, and super.field when a child field hides a parent field."
Common mistakes
- Calling super(...) after other statements in the constructor.