Interview Q&A · Java · 5 questions in this topic
this vs super — Java Interview Q&A
this refers to the current object; super reaches into the superclass. Both double as constructor calls — this(...) and super(...) — with strict first-statement rules. Part of Methods & Constructors · 294 questions in the Java bank.
What is the super keyword?
Answer
super is a reference to the superclass portion of the current object. It lets subclass code reach a hidden field or an overridden method (super.method(), super.field) and invoke a superclass constructor (super(...)). Like this(...), a super(...) call must be the first statement of a constructor.
Example
class Vehicle { void start() { System.out.println("vehicle"); } } class Car extends Vehicle { @Override void start() { super.start(); // run the hidden superclass version System.out.println("car"); } }
What is the difference between this() and super()?
Answer
Both are constructor calls and both must be the first statement of the constructor that contains them. this(...) chains to another constructor of the same class; super(...) chains to a constructor of the direct superclass. Because only one statement can be first, a constructor uses one or the other — never both.
Key points
- 1this(...) → same-class constructor (constructor chaining).
- 2super(...) → superclass constructor; if omitted, the compiler inserts super().
- 3Used as plain references, this means "this object" and super means "the superclass view of this object".
Can a constructor call both this() and super()?
Answer
No. Either a constructor delegates to another constructor of the same class with this(...), or it calls a superclass constructor with super(...), and whichever it is must be the first statement. If neither is written, the compiler inserts a no-argument super() call, which fails to compile when the superclass has no accessible no-argument constructor. Chained constructors form a single chain that ends in exactly one super() call at the top.
Key points
- 1this() and super() are mutually exclusive in one constructor.
- 2Whichever is used must be the first statement.
- 3A missing call means an implicit no-argument super().
- 4The chain reaches exactly one superclass constructor.
Example
class Point { Point() { this(0, 0); } // delegates Point(int x, int y) { super(); } // implicit anyway }
What is the difference between this() and this?
Answer
this refers to the current instance; this() invokes another constructor of the same class. Constructor chaining with this() centralises validation and default values so every construction path goes through one place, instead of duplicating assignments. this(...) must appear as the first statement of the constructor, so a chain cannot also perform work before delegating.
Key points
- 1this = current object reference; this() = sibling constructor call.
- 2this() must be the first statement.
- 3Chaining keeps validation in a single constructor.
- 4Recursive this() chains do not compile.
Why can’t this be used in a static context?
Answer
static members belong to the class, not to an instance, so there is no current object for this to refer to. A static method runs without any instance existing, which is why referring to this, super or an instance field from it is a compile error. The same reasoning explains why a static method cannot be overridden and why it cannot be called polymorphically.
Key points
- 1static code has no implicit instance.
- 2this, super and instance fields are unavailable in static context.
- 3main is static for exactly this reason.
- 4Pass an instance explicitly when static code needs one.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission