Advertisement
Google Ad Slot: content-top
Java super() and this()
In Java, both super() and this() are used to refer to constructors, but they have distinct purposes and rules. Let’s explore each in detail:
super():
- Purpose: Refers to the constructor of the parent (superclass).
- Usage:
- To call the constructor of the parent class.
- It is used when a subclass needs to initialize the fields of the parent class.
this():
- Purpose: Refers to the constructor of the current class.
- Usage:
- To call another constructor from the same class.
- Helps in constructor chaining within the same class.