What is the role of super and this in inheritance?
In Java inheritance, super and this serve different roles:
- super:
-
Refers to the parent class.
-
Used to call the parent class’s constructor, methods, or variables.
-
Example: super() calls the parent class constructor.
-
- this:
-
Refers to the current object (the instance of the class).
-
Used to call current class methods, constructors, or to access class members.
-
Example: this() calls the current class’s constructor.
-
In short:
super accesses the parent class.
this accesses the current class.