****π 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?
Differentiate instance variables and local variables when they have the same name.
Call another constructor in the same class (constructor chaining).
Pass the current object as a parameter to another method or constructor.
Return the current object from a method.
To call current class method
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);
}
}
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();
}
}
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:
To call parent class constructor
β super() is used inside a child class constructor to call the parentβs constructor.To access parent class variables
β If child and parent have same variable names, we can use super.variableName to access the parentβs variable.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");
}
}
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
}
}
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)