Interview Question

What is the difference between this() and super()?

this targets the current instance; super targets direct-superclass behavior.

💡 Concept ✅ Quick Revision ☕ Java

Answer

this refers to the current object; super accesses members or constructors of the direct superclass. • this() invokes another constructor in the same class. • super() invokes a superclass constructor. • A constructor invocation using this() or super() must follow constructor-body rules.

Example

Code
class Parent { String name = "parent"; }
class Child extends Parent {
    String name = "child";
    void show() { System.out.println(this.name + " " + super.name); }
}

Quick Revision

this targets the current instance; super targets direct-superclass behavior.