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
        }
    }
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:
 
- To call the superclass constructor.
- To access a method from the superclass that has been overridden in the subclass.
- 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
        }
    }
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");
        }
    }
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");
        }
    }
DIFFERENT BETWEEN THIS AND SUPER KEYWORD IN JAVA
REFFERED LINKS
 


 
    
Top comments (0)