DEV Community

Vignesh . M
Vignesh . M

Posted on • Edited on

THIS AND SUPER (Keywords)

THIS KEYWORD IN JAVA
* In Java, "this" is a reference variable that refers to the current object, or can be said "this" in Java is a keyword that refers to the current object instance.

It Is Mainly Used To
* Call current class methods and fields
* To pass an instance of the current class as a parameter
* To differentiate between the local and instance variables.

The this keyword is primarily used in the following scenarios:

  • To refer to the current class instance variable.
  • To invoke the current class method.
  • To invoke the current class constructor.
  • To pass the current object as a parameter to a method. To return the current object from a method.
  • To pass the current object as a parameter to a constructor.
  • To access the outer class instance from an inner class. SYNAXT
 class MyClass {
        int value;

        MyClass(int value) {
            this.value = value; // 'this.value' refers to the instance variable
        }
    }

Enter fullscreen mode Exit fullscreen mode

THIS KEYWORD IN JAVA

  • The super keyword in Java is used to refer to the immediate parent class object. It is commonly used to access parent class methods and constructors, enabling a subclass to inherit and reuse the functionality of its superclass.

    • The super keyword can be used in three primary contexts:
  1. To call the superclass constructor.
  2. To access a method from the superclass that has been overridden in the subclass.
  3. To access a field from the superclass when it is hidden by a field of the same name in the subclass.

To access superclass variables when a subclass has a variable with the same name:

class Parent {
        int value = 10;
    }

    class Child extends Parent {
        int value = 20;

        void display() {
            System.out.println("Child's value: " + this.value);
            System.out.println("Parent's value: " + super.value); // Accessing parent's variable
        }
    }
Enter fullscreen mode Exit fullscreen mode

To invoke superclass methods when a subclass overrides the method:

class Parent {
        void show() {
            System.out.println("Parent's show method");
        }
    }

    class Child extends Parent {
        @Override
        void show() {
            super.show(); // Invoking parent's method
            System.out.println("Child's show method");
        }
    }

Enter fullscreen mode Exit fullscreen mode

To invoke the superclass constructor from the subclass constructor:


    class Parent {
        Parent(String message) {
            System.out.println("Parent constructor: " + message);
        }
    }

    class Child extends Parent {
        Child() {
            super("Hello from Child"); // Invoking parent's constructor
            System.out.println("Child constructor");
        }
    }

Enter fullscreen mode Exit fullscreen mode

DIFFERENT BETWEEN THIS AND SUPER KEYWORD IN JAVA

Image description

REFFERED LINKS

  1. https://www.datacamp.com/doc/java/this
  2. https://www.geeksforgeeks.org/java-this-keyword/
  3. https://www.datacamp.com/doc/java/super

Top comments (0)