C

Core Java tutorial for beginners

Clean • Professional

Java – this and super Keywords in Java With Examples

4 minute

this and super Keywords in Java

In Java, the this and super keywords are special references that help manage objects and inheritance in Object-Oriented Programming.

  • this → refers to the current object
  • super → refers to the parent (superclass) object

They are essential for resolving naming conflicts, calling constructors, and accessing overridden methods or variables.


this Keyword

The this keyword refers to the current object — the instance of the class in which it is used.

Common Uses of this :

learn code with durgesh images

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.

Common Uses of super :

learn code with durgesh images

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

this vs super – Quick Comparison Table

Featurethissuper
Refers toCurrent class objectParent class object
AccessInstance variables, methods, constructorsParent variables, methods, constructors
Constructor Callthis() – same classsuper() – parent class
Used ForResolving naming conflicts, passing objectAccessing parent class members
ScopeCurrent object onlyParent (superclass) object

Diagram – this vs super (Conceptual Flow)

            ┌────────────────────────┐
            │        Vehicle         │
            │  color, start()        │
            └──────────┬─────────────┘
                       │
                 super │
                       ▼
            ┌────────────────────────┐
            │          Car           │
            │  color, start(), this  │
            └────────────────────────┘
  • this → refers to Car object (current class)
  • super → refers to Vehicle (parent class)

Points to Remember :

KeywordUsage
thisRefers to current class object. Used to access instance variables, call constructors, or pass current object as a parameter.
superRefers to parent class object. Used to access parent variables, methods, or constructors.
Constructor ChainingUse this() for same class; super() for parent class.
Scopethis → current object; super → parent object.

 

Article 0 of 0