Java super Keyword
Java super Keyword
The super keyword in Java is a reference variable that refers to the immediate parent class object. It is used to access parent class members (variables, methods, and constructors) from a child class.
1. Uses of super Keyword
Access parent class variables
Call parent class methods
Call parent class constructor (constructor chaining)
2. Example 1: Access Parent Class Variables
📌 Output:
super.nameaccesses the parent class variable when overridden by child class.
3. Example 2: Call Parent Class Methods
📌 Output:
super.eat()calls the method from parent class even if it is overridden.
4. Example 3: Call Parent Class Constructor
📌 Output:
super()must be the first statement in the child class constructor.
5. Key Points About super
| Usage | Description |
|---|---|
super.variable | Access parent class variable |
super.method() | Call parent class method |
super() | Call parent class constructor (must be first line) |
| When necessary | Helps avoid ambiguity when members are overridden |
Used in inheritance to access hidden members of parent class.
Can be combined with polymorphism to manage overridden methods.
