this and super Keywords in Java
Introduction to this and super in Java
After learning constructors in Java, the next important concept is understanding how to refer to the current object and the parent class. The this and super keywords are used for this purpose.
These keywords are widely used in object oriented programming, especially with constructors and inheritance.
What is this Keyword in Java
The this keyword in Java refers to the current object of a class. It is used to access instance variables, methods, and constructors of the current class.
It helps remove confusion when local variables and instance variables have the same name.
Uses of this Keyword in Java
Accessing Instance Variables
The this keyword is used to refer to the current class variables.
Calling Current Class Methods
It can be used to call methods of the same class.
Calling Current Class Constructor
It can be used to call another constructor in the same class, known as constructor chaining.
Example of this Keyword
String name;
Student(String name) {
this.name = name;
}
void display() {
System.out.println(this.name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(“Rahul”);
s1.display();
}
}
In this example, this.name refers to the instance variable and resolves the naming conflict.
What is super Keyword in Java
The super keyword in Java refers to the immediate parent class object. It is mainly used in inheritance to access parent class variables, methods, and constructors.
Uses of super Keyword in Java
Accessing Parent Class Variables
super is used when parent and child class variables have the same name.
Calling Parent Class Methods
It allows calling methods of the parent class.
Calling Parent Class Constructor
super can be used to call the constructor of the parent class.
Example of super Keyword
String type = “Animal”;
void show() {
System.out.println(“This is an animal”);
}
}
class Dog extends Animal {
String type = “Dog”;
void display() {
System.out.println(super.type);
super.show();
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.display();
}
}
In this example, super.type refers to the parent class variable and super.show calls the parent class method.
Constructor Chaining in Java
Constructor chaining is the process of calling one constructor from another. The this keyword is used to call a constructor within the same class, while super is used to call the parent class constructor.
Difference Between this and super
The this keyword refers to the current class object, while the super keyword refers to the parent class object. The this keyword is used within the same class, whereas super is used in inheritance.
Advantages of this and super Keywords
These keywords improve code clarity and help manage variable naming conflicts. They make constructor chaining possible and simplify working with inheritance.
Common Mistakes
Using this and super incorrectly can lead to logical errors. Calling both this and super constructors in the same constructor is not allowed. Forgetting inheritance while using super will cause errors.
Interview Questions on this and super Keywords
What is this keyword in Java
What is super keyword in Java
What is the difference between this and super
What is constructor chaining
FAQs
What is this keyword in Java in simple terms
The this keyword refers to the current object of a class.
What is super keyword in Java
The super keyword refers to the parent class object.
Why are this and super used
They are used to access variables, methods, and constructors in current and parent classes.
