1. Introduction
In Java, this and super are two keywords that have special significance in the context of classes and inheritance. The this keyword is a reference to the current object — the object whose method or constructor is being called. The super keyword is used to access methods and constructors of an object's superclass.
2. Key Points
1. this is used to refer to the current class's members (variables, methods, constructors).
2. super is used to refer to the parent class's members.
3. this is commonly used for disambiguation, constructor chaining, and passing the current instance.
4. super is commonly used to override the behavior of a superclass.
3. Differences
this | super |
---|---|
Refers to the current instance of the class. | Refers to the superclass (parent class) of the current instance. |
Used to access or modify the current class's fields, methods, and constructors. | Used to access or invoke the superclass's fields, methods, and constructors. |
It can be used to call other constructors in the same class in constructor chaining. | Used to call the superclass constructor. Often, the first statement is in a subclass constructor. |
It cannot be used in a static context, as it requires an instance. | It cannot be used in a static context, as it refers to the superclass's members, which are inherited by the instance. |
this is often used to eliminate naming conflicts between method parameters and class fields. | super is used to explicitly refer to the overridden methods or fields of a parent class. |
Example use: this.field = field; to differentiate between a class field and a parameter with the same name. | Example use: super.method(); to call a method of the superclass that has been overridden by the subclass. |
4. Example
class ParentClass {
String name;
ParentClass(String name) {
this.name = name;
}
void display() {
System.out.println("Name from ParentClass: " + name);
}
}
class ChildClass extends ParentClass {
String name;
ChildClass(String name, String parentName) {
super(parentName); // Calls the constructor of the ParentClass
this.name = name;
}
void display() {
super.display(); // Calls the display method of ParentClass
System.out.println("Name from ChildClass: " + this.name);
}
}
public class Main {
public static void main(String[] args) {
ChildClass child = new ChildClass("ChildName", "ParentName");
child.display();
}
}
Output:
Name from ParentClass: ParentName Name from ChildClass: ChildName
Explanation:
1. ParentClass has a constructor that initializes the name field using this.name to refer to the current instance's name field.
2. ChildClass extends ParentClass and also has a name field. It uses this.name to refer to its own name field and super(name) to call the superclass's constructor.
3. The display method in ChildClass uses super.display() to invoke the superclass's display method and this.name to refer to its own name field.
5. When to use?
- Use this when you need to refer to the current class’s members or to invoke another constructor of the same class.
- Use super when you need to access members of the superclass or to call the superclass's constructor from a subclass.
Comments
Post a Comment
Leave Comment