this and super keywords

this keyword

this represents the current instance of the class.

Usage

Mostly used inside the constructors or methods to eliminate the confusion between instance variables and parameters with the same name. Please note that a instance variable is shadowed by a method or constructor parameter inside that particular method or constructor.

public class Example{
 
private String name;
 
public Example(String name) {
this.name = name;
}
 
}
 

Used for invoking another constructor inside a constructor.

  1. Please note that this(params) or this() should be the first line in the constructor.
  2. You cannot use this() / this(params) and super()/super(params)  both in the same constructor.
public class Example{
 
private String name;
 
public Example(String name) {
this.name = name;
}
 
public Example() {
this(“Gyan”); // Calling parameterized Constructor
}
 
}
 
super keyword

Usage

  • Access the data members of parent class when both parent and child class have member with same name
  • Explicitly invoke constructors of parent class. super() or super(parameters) should be the first line in the constructor.
  • Access the method of parent class which is overridden in the child class.

Limitations of this and super

this and super cannot be used inside static methods and static blocks.

package com.java.core;

public class Example extends SuperClass {

    private int id = 1;

    private String name = "Gyan";

    public Example(String name) {
        // Compiler will add no-arg super() here
        System.out.println("Invoking Parameterized Constructor for name:" + name);
        this.name = name;
    }
    // Overridden method
    public void method(String name) {
        // Using this to avoid confusion with method parameter having same name
        System.out.println("Method Parameter name:" + name + ", current Object name:" + this.name);
        System.out.println("Method Invoked");
        super.method(name); // Invoking Super Class method using super
    }

    public Example(int id) {
        super(id * 2); // Explicitly Calling Super Class parameterized Constructor
        // Using this to avoid confusion with constructor parameter having same name
        this.id = id;
        System.out.println("Invoking Parameterized Constructor for id:" + id);
    }

    public Example() {
        this(5); // Calling parameterized Constructor
        System.out.println("Invoking No args Constructor");
    }

    public void printIds() {
        System.out.println("Current instance id:" + this.id + ", name:" + name);
        System.out.println("Super instance id:" + super.id);
    }

    public static void main(String[] args) {
        System.out.println("-------------------------------------------------------");
        Example example1 = new Example();
        example1.printIds();
        example1.method("Rochit");
        System.out.println("-------------------------------------------------------");
        Example example2 = new Example(10);
        example2.printIds();
        System.out.println("-------------------------------------------------------");
        Example example3 = new Example("Ram");
        example3.printIds();
        System.out.println("-------------------------------------------------------");

    }

}

class SuperClass {

    int id;

    public SuperClass() {
        System.out.println("Invoking No args SuperClass Constructor");
        this.id = 10;
    }

    public SuperClass(int id) {
        System.out.println("Invoking Parameterized SuperClass Constructor");
        this.id = id;
    }

    public void method(String name) {
        System.out.println("Super Class Method Invoked");
    }

}
-------------------------------------------------------
Invoking Parameterized SuperClass Constructor
Invoking Parameterized Constructor for id:5
Invoking No args Constructor
Current instance id:5, name:Gyan
Super instance id:10
Method Parameter name:Rochit, current Object name:Gyan
Method Invoked
Super Class Method Invoked
-------------------------------------------------------
Invoking Parameterized SuperClass Constructor
Invoking Parameterized Constructor for id:10
Current instance id:10, name:Gyan
Super instance id:20
-------------------------------------------------------
Invoking No args SuperClass Constructor
Invoking Parameterized Constructor for name:Ram
Current instance id:1, name:Ram
Super instance id:10
-------------------------------------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *