📘 Lesson · Lesson 28
this and super
this and super
this and super
this refers to the current object. super refers to the parent class — used to call parent methods or constructors.
this Example
class Student {
String name;
Student(String name) {
this.name = name; // this distinguishes field from parameter
}
}
super Example
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
void sound() {
super.sound(); // calls parent method
System.out.println("Bark");
}
}Some sound
Bark
Summary
this= current object (resolves name conflicts).super= parent class (call parent method/constructor).
this और super
this current object को refer करता है। super parent class को — parent methods या constructors call करने को।
this Example
class Student {
String name;
Student(String name) {
this.name = name; // field को parameter से अलग करता है
}
}
super Example
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
void sound() {
super.sound(); // parent method call करता है
System.out.println("Bark");
}
}Some sound
Bark
सारांश
this= current object (name conflicts हल करता है)।super= parent class (parent method/constructor call)।