this and super Keywords in Java
In Java, the this and super keywords are special references that allow objects to refer to themselves and their parent class, respectively. They are commonly used in object-oriented programming to manage variables, constructors, and methods efficiently.
this Keyword
The this keyword refers to the current object — the instance of the class in which it is used.
Uses of this

1. Referencing Instance Variables
When local variables or method parameters shadow instance variables, this helps distinguish them.
class Student {
int rollNo;
String name;
Student(int rollNo, String name) {
this.rollNo = rollNo; // this.rollNo refers to instance variable
this.name = name;
}
void display() {
System.out.println("Roll No: " + this.rollNo + ", Name: " + this.name);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student(101, "Alice");
s.display();
}
}
Output:
Roll No: 101, Name: Alice
2. Calling Another Constructor (Constructor Chaining)
You can use this() to call another constructor within the same class.
class Car {
String color;
int speed;
Car() {
this("White", 0); // Calls parameterized constructor
}
Car(String color, int speed) {
this.color = color;
this.speed = speed;
}
void display() {
System.out.println(color + " car at " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.display();
}
}
Output:
White car at 0 km/h
3. Passing Current Object as a Parameter
this can be passed as an argument to methods or constructors.
class Display {
void show(Student s) {
System.out.println("Student name: " + s.name);
}
}
class Student {
String name;
Student(String name) {
this.name = name;
}
void print() {
Display d = new Display();
d.show(this); // Passing current object
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Alice");
s.print();
}
}
Output:
Student name: Alice
super Keyword
The super keyword refers to the parent (superclass) object. It is mainly used in inheritance.
Uses of super

1. Accessing Parent Class Variables
class Vehicle {
String color = "White";
}
class Car extends Vehicle {
String color = "Red";
void display() {
System.out.println("Car color: " + color);
System.out.println("Vehicle color: " + super.color); // parent class variable
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.display();
}
}
Output:
Car color: Red
Vehicle color: White
2. Calling Parent Class Constructor
super() can be used to invoke a parent class constructor.
class Vehicle {
Vehicle() {
System.out.println("Vehicle constructor called");
}
}
class Car extends Vehicle {
Car() {
super(); // Calls Vehicle constructor
System.out.println("Car constructor called");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
}
}
Output:
Vehicle constructor called
Car constructor called
Note: If you don’t explicitly call super(), Java calls the default parent constructor automatically.
3. Calling Parent Class Methods
You can invoke a method from the parent class using super.methodName().
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
class Car extends Vehicle {
void start() {
super.start(); // Calls Vehicle's start method
System.out.println("Car starts");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Output:
Vehicle starts
Car starts
Points to Remember
| Keyword | Usage |
|---|---|
this | Refers to current class object. Used to access instance variables, call constructors, or pass current object as a parameter. |
super | Refers to parent class object. Used to access parent variables, methods, or constructors. |
| Constructor Chaining | Use this() for same class; super() for parent class. |
| Scope | this → current object; super → parent object. |
🏆 Top 5 Interview Questions - this and super Keywords
1. What is the purpose of the this keyword in Java?
Answer: Refers to the current object of the class.
- Used to:
- Access instance variables when shadowed by local variables.
- Call another constructor of the same class (
this()). - Pass the current object as a parameter to methods or constructors.
2. What is the purpose of the super keyword in Java?
Answer: Refers to the parent (superclass) object.
- Used to:
- Access parent class variables.
- Call parent class methods (
super.methodName()). - Invoke parent class constructor (
super()).
3. What is constructor chaining using this() and super()?
Answer: Constructor chaining allows one constructor to call another constructor.
this()→ Calls another constructor in the same class.super()→ Calls a constructor of the parent class.- Helps avoid code duplication and initialize objects efficiently.
4. Can super() be omitted in a subclass constructor?
Answer: Yes, if not explicitly called, Java automatically calls the default constructor of the parent class. If the parent class has no default constructor, super() must be called explicitly.
5. Difference between this and super keywords?
Answer:
| Feature | this | super |
|---|---|---|
| Refers to | Current class object | Parent class object |
| Access | Instance variables, methods, constructors | Parent variables, methods, constructors |
| Constructor Call | this() calls another constructor in same class | super() calls parent constructor |
| Use Case | Resolve naming conflicts, pass object, constructor chaining | Access overridden members, call parent constructor |
