this and super keyword interview questions, with answers
this and super are two small keywords that appear in a surprising number of fresher-round questions, because each one has a rule that produces a compile error or a silent bug when it is misunderstood: a setter that assigns a parameter to itself, a super() call that is not the first statement, a static method that tries to use this.
The questions below cover those rules with the code that shows them. When you have read them, take the free OOP diagnostic — ten questions across all fifteen OOP topics, scored with the arithmetic shown.
The questions, with answers
1.What does this refer to, and why does a setter written as x = x do nothing?
Inside an instance method or constructor, this is a reference to the object the method was called on — the receiver of the call. Its most common job is resolving a name clash: when a parameter or local variable has the same name as a field, the local one shadows the field, so in void setX(int x) { x = x; } both sides refer to the parameter and the field is never touched. Writing this.x = x tells the compiler the left side means the field. The compiler does not flag the useless assignment as an error (some IDEs warn), which is why the bug survives into interviews as a favourite "what does this code do" question.
class Point { private int x; void setX(int x) { this.x = x; } // without this., the parameter is assigned to itself }2.What is the difference between this() and super() in a constructor?
Both are constructor calls that must be the first statement of a constructor, and a constructor may contain at most one of them. this(...) calls another constructor of the same class, which is how overloaded constructors share initialisation without duplicating it. super(...) calls a constructor of the immediate superclass, so the parent part of the object is initialised before the child's body runs; if you write neither, the compiler inserts super() with no arguments. Because a constructor that starts with this(...) ends up in a constructor that calls super(...), every chain still reaches the parent exactly once. The first-statement rule is what interviewers test: putting a print before super(...) was a compile error in every Java up to 24. Java 25's flexible constructor bodies now allow statements before the call as long as they do not touch the object being built, but the call must still happen exactly once, and the classic rule is what interviews expect you to know.
class Account { double balance; Account() { this(0.0); } // delegate to the other constructor Account(double b) { super(); balance = b; } // super() explicit here, implicit otherwise }3.When does a constructor fail to compile because of super()?
When the superclass has no no-argument constructor and the subclass constructor does not call super with arguments explicitly. If Vehicle declares only Vehicle(String reg), then class Car extends Vehicle with a plain Car() constructor gets an implicit super(), there is no Vehicle() to call, and the compiler reports it. The fix is Car() { super("unknown"); } or a matching Car(String reg) { super(reg); }. The same rule bites when you add a parameterised constructor to a class that already has subclasses — their implicit super() calls break — and it is why library base classes usually keep a no-argument constructor even when they add others.
4.How do you call the parent's version of a method you have overridden?
With super.method(). Inside an override, a plain method() call dispatches to the most-derived version — which is the override itself, so calling it would recurse — while super.method() calls the immediate parent's implementation directly, with no dynamic dispatch. The usual pattern is to extend rather than replace: an overriding toString() or close() that calls super's version first and adds its own work. If class A's show() prints "A" and class B's override prints super.show() followed by "B", calling show() on a B prints A then B. You cannot skip a level — super.super.method() is not legal — and super cannot be used in a static method, because there is no object to be the parent part of.
5.Can you pass this as an argument or return it from a method?
Yes, and both are common idioms. Passing this registers the current object with someone else — button.addListener(this) hands the listener the object that wants callbacks, and a node might insert itself into a parent's list with parent.add(this). Returning this enables method chaining, which is how builders and fluent APIs read: builder.name("x").age(3).build() works because each setter returns the builder. The caution interviewers look for is leaking this from a constructor: passing this to another thread or a registry before the constructor finishes exposes a half-built object, and in Java it also defeats the guarantees of final fields. Hand out this only once the object is fully initialised.
class QueryBuilder { private String table, where; QueryBuilder from(String t) { table = t; return this; } // returning this enables chaining QueryBuilder where(String w) { where = w; return this; } } // new QueryBuilder().from("orders").where("total > 100")6.Why can't this be used in a static method?
Because a static method belongs to the class and runs without any object; there is no receiver for this to name. Using this, or an instance field or method without an explicit object, inside a static method is a compile error — the message is "non-static variable this cannot be referenced from a static context". main is static, so the classic beginner error is calling an instance method from main without creating an object first. The fix is to construct an instance and call through it, or to make the method static if it genuinely needs no object state. super is barred for the same reason: with no object there is no parent portion to refer to.
7.What does this mean inside an anonymous class, an inner class or a lambda?
Inside an anonymous class or a non-static inner class, this is the anonymous or inner object itself, not the enclosing one — to reach the outer instance you write Outer.this. That surprises people who write a listener as an anonymous class and find this.field refers to the listener. Lambdas are the opposite: a lambda has no this of its own, so this inside a lambda is the enclosing instance, which is one of the practical reasons lambdas replaced anonymous classes for callbacks. Static nested classes have no enclosing instance at all, so Outer.this does not exist there. Interviewers use a snippet with a nested this to check whether you know these three cases apart.
8.How does this differ in C++, Python and JavaScript?
In C++ this is a pointer, not a reference, so members are reached with this->x, and *this is the object itself — returning *this by reference is how operator= and chaining work. Python has no keyword: the receiver is the first parameter of every method, conventionally named self, passed explicitly, so forgetting it in a method signature is the classic error. JavaScript's this is decided by how a function is called, not where it is defined: a method call binds it to the object, a plain call leaves it undefined in strict mode, and arrow functions capture the this of the surrounding scope rather than having their own — the same rule as Java lambdas. If an interviewer mixes languages, name which rule you are describing before you answer.
How the diagnostic asks it
One question from the OOP bank, exactly as a sitting would show it. The bank has 4 on this/super keyword and 60 across OOP.
What does the 'this' keyword refer to inside a non-static instance method?
- 1The most recently created object of any class in the program.
- 2The parent class of the current object.
- 3The current object instance on which the method was invoked.correct
- 4The class definition itself, independent of any object.
'this' is a reference to the current object instance, commonly used to distinguish instance fields from parameters with the same name or to pass the current object elsewhere. A reference to the parent class is what 'super' provides (in languages that support it); 'this' is never the class definition independent of an object, and it has nothing to do with the most recently created object in the program.
Measure it
Reading answers tells you what’s true. A diagnostic tells you what you get wrong.
10 OOP questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.