DEV Community

Arun
Arun

Posted on

This and Super keyword

****πŸ‘‰ What is this keyword in Java?

βœ“βœ“ this is a reference variable in Java.

βœ“βœ“ It always refers to the current object of the class.

βœ“βœ“ Used to avoid confusion and to make code clear.

βœ“βœ“ this can be used to call constructors or methods of the same class.

βœ“βœ“ this represents the object through which the method or constructor is called.

,πŸ‘‰ Why we use this keyword?

  1. Differentiate instance variables and local variables when they have the same name.

  2. Call another constructor in the same class (constructor chaining).

  3. Pass the current object as a parameter to another method or constructor.

  4. Return the current object from a method.

  5. To call current class method

  6. To call current class constructor

πŸ”ΉπŸ‘‰ When we use this keyword?

βœ“βœ“ When local variables and instance variables have the same name.

βœ“βœ“ When we want to call another constructor inside the same class.

βœ“βœ“ When we need to refer to the current object inside a method or constructor.

πŸ”Ή Example Program (using this keyword to differentiate variables)

class Student {
int rollno;
String name;

// Constructor with local variables having same names as instance variables
Student(int rollno, String name) {
    this.rollno = rollno;   // 'this' refers to instance variable
    this.name = name;       // differentiates instance variable from local variable
}

void display() {
    System.out.println("Roll No: " + this.rollno);
    System.out.println("Name: " + this.name);
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Student s1 = new Student(101, "Arun");
Student s2 = new Student(102, "Kumar");

    s1.display();
    s2.display();
}
Enter fullscreen mode Exit fullscreen mode

}

Output
Roll No: 101
Name: Arun
Roll No: 102
Name: Kumar

**

πŸ‘‰ What is super keyword?

βœ“βœ“ super is a reference variable in Java.

βœ“βœ“ It is used to refer to the immediate parent class object.

πŸ‘‰ Why super keyword is used?

We use super mainly for three purposes:

  1. To call parent class constructor
    β†’ super() is used inside a child class constructor to call the parent’s constructor.

  2. To access parent class variables
    β†’ If child and parent have same variable names, we can use super.variableName to access the parent’s variable.

  3. To access parent class methods
    β†’ If child overrides a parent method, we can call the parent’s method using super.methodName().

πŸ‘‰ When super is used?

βœ“βœ“ Inside a constructor (to call parent’s constructor).

βœ“βœ“ Inside a method (to call parent’s variable or parent’s method).

βœ“βœ“ When there is overriding or same variable names in parent and child.

Example Program

class Parent {
int value = 100;

Parent() {
    System.out.println("Parent Constructor Called");
}

void display() {
    System.out.println("Parent display() method");
}
Enter fullscreen mode Exit fullscreen mode

}

class Child extends Parent {
int value = 200;

Child() {
    super(); // calls Parent constructor
    System.out.println("Child Constructor Called");
}

void display() {
    super.display(); // calls Parent method
    System.out.println("Child display() method");
}

void showValues() {
    System.out.println("Child value: " + value);        // child variable
    System.out.println("Parent value: " + super.value); // parent variable
}
Enter fullscreen mode Exit fullscreen mode

}

public class SuperDemo {
public static void main(String[] args) {
Child c = new Child();
c.display();
c.showValues();
}
}

Output

Parent Constructor Called
Child Constructor Called
Parent display() method
Child display() method
Child value: 200
Parent value: 100

**

Top comments (0)