super keywords
super
Is a reference variable in Java that refers to a direct parent class object.
Main uses:
-
Accessing member variables of parent class: When the subclass and parent class have variables with the same name
;
-
Calling the parent class method: Especially when the subclass overrides the parent class method
();
-
Call the constructor of the parent class: Must be in the first line of the subclass constructor
super(); // orsuper(parameters);
This keyword
this
Is a reference variable in Java that refers to the current object.
Main uses:
-
Refer to the member variable of the current object: Solve the problem of the same name as the local variable and the member variable
= variableName;
-
Call the method of the current class: Clearly call the method of the current class
();
-
Call the constructor of the current class: Must be in the first line of the constructor
this(); // orthis(parameters);
-
Passed as parameters: Pass the current object as a parameter
method(this);
The difference between super and this
characteristic | super |
this |
---|---|---|
Reference object | Parent class object | Current object |
Use scenarios | Mainly used in inheritance relationships | Can be used in any class |
Call the constructor | Must be in the first row of the subclass constructor | Must be in the first row of the constructor |
Access permissions | Only members visible to the parent class are accessible | All members of the current class can be accessed |
Chain call | Cannot call in chain ( illegal) |
Can be called in chain ( illegal) |
Static context | Cannot be used in static methods/blocks | Cannot be used in static methods/blocks |
Code Example
class Parent { String name = "Parent"; void display() { ("Parent's display"); } } class Child extends Parent { String name = "Child"; Child() { this("Default"); // Call another constructor of this class } Child(String name) { super(); // Call the parent class constructor = name; } void display() { ("Child's display"); } void show() { (); // Access the name of the parent class (); // Access the name of this class (); // Call the display of the parent class (); // Call the display of this class } } public class Main { public static void main(String[] args) { Child child = new Child("Test"); (); } }
Output result:
Parent
Test
Parent's display
Child's display
Summarize
use
super
It is mainly to explicitly access the members of the parent class in the subclass to avoid confusion with the members of the subclass.use
this
It is mainly to explicitly access the members of the current object within the class to avoid confusion with local variablesBoth can be used for constructor calls, but both must be located in the first line of the constructor
In a static context, neither can be used
This is the article about the use and differences between super and this keywords in Java. For more related contents of super and this keywords in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!