Super Keyword in Java — Java Technical Interview Questions & Answers
Super Keyword in Java
Super Keyword in Java
- super talks to the parent from the child. super.method() calls the parent version. super() calls a parent constructor and must be the first statement in the child constructor.
- Use super when the child overrides but still wants parent behaviour too. If the parent has no no-arg constructor and you skip super(...), you get a compile error.
- Putting any code before super() in a constructor is illegal. Interview line: super.method for parent method; super() for parent constructor first.
- Let's take this on the board with one tiny Main class — no extra files. Super Keyword in Java — output: Parent then Child. super.show() runs Parent first; then Child prints its own line. Start from main, go line by line, and stop at each print. That output is the proof for Super Keyword.
Technical Interview Tip: super() must be the first statement in the constructor.
Code Examples for Super Keyword in Java
Example
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
@Override
void show() {
super.show();
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
new Child().show();
}
}Super Keyword in Java — output: Parent then Child. super.show() runs Parent first; then Child prints its own line.
How to Explain Super Keyword in Java to an Interviewer
What does super.method() do?
Calls the parent version
Where must super() sit in a constructor?
First statement
When do you need super(...)?
Parent has no no-arg constructor