The super keyword in Java is used to refer to the immediate parent class object.
In simple words:
The super keyword allows a child class to access parent class variables, methods, and constructors.
Why super Keyword is Important?
The super keyword helps:
- Access parent class variables
- Call parent class methods
- Invoke parent class constructors
- Support inheritance properly
- Avoid ambiguity between parent and child members
Main Uses of super Keyword
- Access parent class variable
- Call parent class method
- Call parent class constructor
super Keyword Flow Diagram
Child Class
|
+-------> Access Parent Variable
|
+-------> Call Parent Method
|
+-------> Call Parent Constructor
1. Access Parent Class Variable Using super
When parent and child classes have variables with same name, super is used to access parent variable.
Example
class Parent {
int number = 100;
}
class Child extends Parent {
int number = 200;
void display() {
System.out.println(super.number);
System.out.println(this.number);
}
}
Output
100 200
Explanation
- super.number → Parent variable
- this.number → Child variable
Variable Access Flow
Child Object
|
+-------> this.number → Child Variable
|
+-------> super.number → Parent Variable
2. Call Parent Class Method Using super
super can call parent class methods when child overrides them.
Example
class Parent {
void show() {
System.out.println("Parent Method");
}
}
class Child extends Parent {
void show() {
System.out.println("Child Method");
}
void display() {
super.show();
show();
}
}
Output
Parent Method Child Method
Explanation
- super.show() → Calls parent method
- show() → Calls child method
Method Call Flow Diagram
Child Class Method
|
+-------> super.show()
|
+-------> Parent Method Executes
|
+-------> Child Method Executes
3. Call Parent Constructor Using super()
super() is used to call parent class constructor.
Example
class Parent {
Parent() {
System.out.println("Parent Constructor");
}
}
class Child extends Parent {
Child() {
super();
System.out.println("Child Constructor");
}
}
Output
Parent Constructor Child Constructor
Why Parent Constructor Executes First?
Because parent class properties and methods must initialize before child initialization.
Constructor Chaining Flow
Child Constructor
|
v
super()
|
v
Parent Constructor
|
v
Child Constructor Executes
Important Rule of super()
The super() call must always be:
- First statement inside constructor
Invalid Example
Child() {
System.out.println("Hello");
super();
}
Why Invalid?
Because super() is not the first statement.
Implicit super() Call
If constructor does not explicitly call super(), Java automatically inserts:
super();
Example
class Parent {
Parent() {
System.out.println("Parent");
}
}
class Child extends Parent {
Child() {
System.out.println("Child");
}
}
Actual Internal Working
class Child extends Parent {
Child() {
super();
System.out.println("Child");
}
}
super with Parameterized Constructor
class Vehicle {
Vehicle(String type) {
System.out.println(type);
}
}
class Car extends Vehicle {
Car() {
super("Car");
}
}
Execution Flow
Car()
|
v
super("Car")
|
v
Vehicle Constructor Executes
Difference Between this and super
| Feature | this | super |
|---|---|---|
| Represents | Current Class Object | Parent Class Object |
| Used For | Current Class Access | Parent Class Access |
| Constructor Call | this() | super() |
| Method Access | Current Method | Parent Method |
super Keyword in Banking Systems
Banking applications use super keyword for:
- Base transaction initialization
- Security configurations
- Common account setup
- Audit logging systems
Example
TransactionService
|
+-------> PaymentService
|
+-------> LoanService
|
+-------> AccountService
Child services use:
super()
to initialize common transaction behavior.
super Keyword in E-Commerce Systems
E-commerce systems use super keyword for:
- Base order processing
- Inventory management
- Payment systems
- Notification frameworks
super Keyword in Spring Boot
Spring Boot frameworks use super keyword internally for:
- Security configurations
- Exception handling
- REST controller extensions
- Bean customization
Spring Boot Example
@Override
protected void configure(
HttpSecurity http
) throws Exception {
super.configure(http);
}
super Keyword Internal Working
Child Object Created
|
v
Parent Constructor Called
|
v
Parent Members Initialized
|
v
Child Members Initialized
Advantages of super Keyword
- Supports inheritance properly
- Avoids ambiguity
- Enables constructor chaining
- Improves code reuse
- Supports polymorphism
Disadvantages
- Deep inheritance may increase complexity
- Excessive parent dependency reduces flexibility
Common Interview Mistake
Many developers think super can access private parent members.
Actually:
- Private members are not accessible using super.
Another Common Mistake
Many developers think both this() and super() can exist together.
Actually:
- Only one constructor call is allowed.
- It must be first statement.
Invalid Example
Child() {
this();
super();
}
Best Practices
- Use super for meaningful parent access
- Avoid unnecessary inheritance
- Use constructor chaining properly
- Keep inheritance hierarchy clean
super Keyword in Microservices
Microservices architectures use super keyword for:
- Base service initialization
- Security framework setup
- Logging infrastructure
- Cloud configuration setup
Realtime Enterprise Example
Base Service
BaseNotificationService
Child Services
- EmailNotificationService
- SMSNotificationService
- PushNotificationService
Child services call:
super()
to initialize common notification configurations.
Related Learning Topics
- What is this Keyword in Java
- What is Constructor Chaining
- What is Inheritance in Java
- What is Method Overriding
- OOPS in Java
- What is Spring Boot
- What are Microservices
Professional Interview Answer
The super keyword in Java is used to refer to the immediate parent class object. It is mainly used to access parent class variables, invoke parent class methods, and call parent class constructors. The super() constructor call supports constructor chaining and ensures proper initialization of inherited members. The super keyword is heavily used in inheritance-based architectures, Spring Boot frameworks, enterprise applications, banking systems, microservices platforms, and object-oriented designs to reuse parent class functionality and maintain clean inheritance hierarchies.
Frequently Asked Questions
What is super keyword in Java?
super refers to the immediate parent class object.
What are the uses of super keyword?
Access parent variables, call parent methods, and invoke parent constructors.
Can super access private members?
No, private parent members cannot be accessed using super.
Why is super() used?
super() is used to call parent class constructor.
Can this() and super() be used together?
No, only one constructor call is allowed inside a constructor.