this and super Keywords in Java – Complete Detailed Tutorial
Java provides two special keywords — this and super — to refer to objects and members in classes and inheritance hierarchies.
1. this Keyword in Java
The this keyword refers to the current object of a class. It is commonly used for:
- Referencing instance variables
- Calling another constructor (constructor chaining)
- Passing the current object as a parameter
- Invoking current class methods
1.1 Referencing Instance Variables
- Used when local variable and instance variable have the same name
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name; // refers to instance variable
this.age = age;
}
void display() {
System.out.println("Name: " + this.name + ", Age: " + this.age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Chinmaya", 25);
s1.display();
}
}
Output:
1.2 Constructor Chaining
- Call one constructor from another using
this()
class Student {
String name;
int age;
Student() { // default constructor
this("Unknown", 18); // calls parameterized constructor
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Output:
1.3 Passing Current Object as Parameter
class Printer {
void print(Student s) {
System.out.println("Printing Student: " + s.name);
}
}
class Student {
String name;
Student(String name) {
this.name = name;
}
void show() {
Printer p = new Printer();
p.print(this); // pass current object
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Chinmaya");
s1.show();
}
}
Output:
Printing Student: Chinmaya
1.4 Invoking Current Class Method
class Student {
void greet() {
System.out.println("Hello from greet()");
}
void show() {
this.greet(); // calls current class method
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.show();
}
}
Output:
2. super Keyword in Java
The super keyword refers to the parent class (superclass). It is used for:
- Accessing parent class methods
- Accessing parent class variables
- Calling parent class constructor
2.1 Accessing Parent Class Method
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void eat() {
System.out.println("Dog is eating");
}
void show() {
super.eat(); // call parent class method
eat(); // call current class method
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.show();
}
}
Output:
Animal is eating
Dog is eating
2.2 Accessing Parent Class Variable
class Animal {
String color = "White";
}
class Dog extends Animal {
String color = "Black";
void printColor() {
System.out.println(color); // child class variable
System.out.println(super.color); // parent class variable
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.printColor();
}
}
Output:
2.3 Calling Parent Class Constructor
class Animal {
Animal() {
System.out.println("Animal Constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // calls parent class constructor
System.out.println("Dog Constructor");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
}
}
Output:
Animal Constructor
Dog Constructor
3. Summary
| KeywordPurpose |
this | Refers to current object, used for variables, methods, constructor chaining, passing object |
super | Refers to parent class, used for variables, methods, and constructors |
Key Points:
this() → calls current class constructorsuper() → calls parent class constructorthis is optional, but improves readability and clarity