Clean • Professional
this and super Keywords in JavaIn Java, the this and super keywords are special references that help manage objects and inheritance in Object-Oriented Programming.
this → refers to the current objectsuper → refers to the parent (superclass) objectThey are essential for resolving naming conflicts, calling constructors, and accessing overridden methods or variables.
this KeywordThe this keyword refers to the current object — the instance of the class in which it is used.
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 KeywordThe super keyword refers to the parent (superclass) object. It is mainly used in inheritance.
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
| Feature | this | super |
|---|---|---|
| Refers to | Current class object | Parent class object |
| Access | Instance variables, methods, constructors | Parent variables, methods, constructors |
| Constructor Call | this() – same class | super() – parent class |
| Used For | Resolving naming conflicts, passing object | Accessing parent class members |
| Scope | Current object only | Parent (superclass) object |
┌────────────────────────┐
│ Vehicle │
│ color, start() │
└──────────┬─────────────┘
│
super │
▼
┌────────────────────────┐
│ Car │
│ color, start(), this │
└────────────────────────┘this → refers to Car object (current class)super → refers to Vehicle (parent class)| 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. |