this vs super Keyword in Java
Compare this and super in Java — how this references the current object and super reaches the parent class, with constructor chaining and interview Q&A.
Expected Interview Answer
In Java, this refers to the current object and its own members, while super refers to the immediate parent class, letting a subclass access the parent's members and constructors.
this is used to reference the current instance's fields and methods, to disambiguate a field from a parameter of the same name, and to call another constructor in the same class with this(). super is used to access a parent's overridden method or hidden field and to invoke the parent constructor with super(). Both this() and super() must be the first statement in a constructor, so only one of them can appear per constructor.
- this resolves naming conflicts between fields and parameters
- super reuses parent behaviour instead of duplicating it
- this() and super() enable constructor chaining
- super lets a subclass extend rather than replace parent logic
- Both make inheritance relationships explicit and readable
AI Mentor Explanation
this is a batter pointing to his own kit bag, and super is that same batter reaching for the team locker his senior set up. When he needs his personal gloves he uses his own bag, but for the shared bat oil prepared by the veteran he opens the parent locker. Java uses this for the current object's members and super to reach what the parent class provides.
Step-by-Step Explanation
Step 1
Use this for current members
Reference the current object's fields and methods, e.g. this.name or this.calculate().
Step 2
Disambiguate names
When a parameter shadows a field, use this.field = field to assign correctly.
Step 3
Chain constructors with this()
Call another constructor of the same class as the first statement using this(args).
Step 4
Use super for parent members
Access an overridden method or hidden field of the parent with super.method().
Step 5
Call the parent constructor
Invoke the parent's constructor with super(args) as the first statement in a subclass constructor.
What Interviewer Expects
- That this refers to the current object and super to the parent class
- How this resolves shadowing between fields and parameters
- Constructor chaining with this() and super()
- That this()/super() must be the first statement in a constructor
- When super is needed to reach an overridden method
Common Mistakes
- Thinking super refers to a grandparent rather than the immediate parent
- Placing this() or super() somewhere other than the first line
- Using both this() and super() in the same constructor
- Forgetting this. when a parameter shadows a field
- Assuming super can access private members of the parent
Best Answer (HR Friendly)
“this points to the object you are currently working with, so you use it to reach that object's own data and methods. super points to the class it was built on top of, so you use it to reuse the parent's behaviour or set-up instead of rewriting it.”
Code Example
class Animal {
String name;
Animal(String name) {
this.name = name; // this resolves the shadowed field
}
void speak() {
System.out.println(name + " makes a sound");
}
}
class Dog extends Animal {
Dog(String name) {
super(name); // call parent constructor
}
@Override
void speak() {
super.speak(); // reuse parent behaviour
System.out.println(this.name + " barks");
}
}
public class Main {
public static void main(String[] args) {
new Dog("Rex").speak();
}
}Follow-up Questions
- Why must this() or super() be the first statement in a constructor?
- Can you use both this() and super() in one constructor?
- How does this help when a parameter shadows a field?
- When would you call super.method() from an override?
- What happens if you don't explicitly call super() in a subclass constructor?
MCQ Practice
1. The super keyword is used to access members of:
super refers to the immediate parent (superclass) of the current class.
2. Where must this() or super() appear in a constructor?
A constructor call with this() or super() must be the very first statement, so only one can appear.
3. What is this.name = name commonly used for?
When a parameter shadows a field, this.name refers to the field while name is the parameter.
Flash Cards
What does this refer to? — The current object — its own fields, methods, and constructors.
What does super refer to? — The immediate parent class — its members and constructor.
What does this() do? — Calls another constructor in the same class (constructor chaining).
What does super() do? — Calls the parent class's constructor from a subclass constructor.
Can this() and super() coexist in one constructor? — No — each must be the first statement, so only one is allowed.