this and super Keywords

Thinking

The this and super keywords provide a way to refer to the current class instance and the parent class respectively.

Understanding these keywords is essential to manage class inheritance and object references efficiently in Java.

Description

this is a reference variable in Java that refers to the current object.

super is a reference variable used to refer to the immediate parent class object.

  • Usage of this keyword:
    • To refer to current class instance variables when they are shadowed by method parameters.
    • To invoke current class methods.
    • To call current class constructors (constructor chaining).
  • Usage of super keyword:
    • To access parent class variables if they are hidden by child class variables.
    • To invoke parent class methods overridden in the child class.
    • To call parent class constructors.

Video Resources

Java this Keyword

Explains how the this keyword works in Java.

Examples (code)

Example Using this Keyword


class Student {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;         // 'this' distinguishes instance variable from parameter
        this.name = name;
    }

    void display() {
        System.out.println("ID: " + this.id + ", Name: " + this.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s = new Student(101, "John");
        s.display();
    }
}
  

Example Using super Keyword


class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }

    void parentSound() {
        super.sound();   // calls Animal's sound method
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
        d.parentSound();
    }
}
  

Example Using super Constructor


class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}

class Employee extends Person {
    int id;

    Employee(String name, int id) {
        super(name);   // calls Person's constructor
        this.id = id;
    }

    void display() {
        System.out.println("Name: " + name + ", ID: " + id);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee e = new Employee("Alice", 1001);
        e.display();
    }
}
  

Real-World Applications

Object Initialization

Using this to avoid ambiguity during object creation in complex classes.

Inheritance Hierarchy

super helps access overridden methods and parent constructors in subclass hierarchies.

Framework Development

Frameworks use this and super to manage code reuse and override behavior efficiently.

Interview Questions

Q1: What is the purpose of the this keyword in Java?

Show Answer

The this keyword refers to the current object instance and is commonly used to resolve naming conflicts between instance variables and parameters.

Q2: How is the super keyword used in Java?

Show Answer

super is used to access parent class members (variables, methods) and to call parent class constructors from a subclass.

Q3: Can you call one constructor from another using this?

Show Answer

Yes, using this() you can call another constructor of the same class to reuse initialization code (constructor chaining).