---
title: Super Keyword in Java - Scaler Topics
description: Learn about Super Keyword in Java by Scaler Topics. This article defines the java super keyword and its different types of characteristics.
author: Aayush
category: Java
---
:::section{.main}
The `super` keyword in Java is used as a reference variable to access objects from parent classes. It allows access to parent class constructors, members, and methods in the derived class. The keyword plays a crucial role in inheritance and polymorphism concepts.
In this article, we have discussed the usage of the super keyword in Java, along with real-life examples.
:::
:::section{.main}
## Usage of Java super Keyword

### To Refer Immediate Parent Class Instance Variable
In this scenario, we have two classes: `School` and `Teacher`, which store information about schools in a specific region and the teachers working in those schools. The `Teacher` class is a subclass of the `School` class.
Both classes have a `name` variable of type `String`. We will now explore how to access the `name` variable of the parent class, `School`.
**Example:**
```java
// Parent class
class School {
// Instance variables
int id;
String name = "SSM Public School";
}
class Teacher extends School {
// Instance variable in the child class
int id;
String name;
void printSchoolName() {
// Using super.name, we can access the name variable of the parent class
// Using this.name, we can access the name variable of the child class
System.out.println("School Name: " + super.name);
}
public static void main(String[] args) {
Teacher ob = new Teacher();
ob.printSchoolName();
}
}
```
**Output:**
```plaintext
School Name: SSM Public School
```
**Explanation:**
* Two classes are created: `School` and `Teacher`, with `Teacher` extending `School`.
* Both classes have `id` and `name` variables.
* The `super` keyword references the `id` and `name` variables of the parent class.
* To access the `id` variable of the `School` class within the `Teacher` class, we can use `super.id`.
### To Invoke Immediate Parent Class Method
Consider the same example we discussed above. Now, both the base and derived classes contain the same method, `printID()`. Let's see how we can access the `printID()` method of the parent class inside the child class.
**Example:**
```java
class School {
// Instance variables
int id = 1;
String name = "SSM Public School";
void printID() {
System.out.println("School ID: " + this.id);
}
}
class Teacher extends School {
// Instance variable in the child class
int id = 10;
void printSchoolName() {
System.out.println("School Name: " + super.name);
}
void printID() {
System.out.println("Teacher ID: " + this.id);
super.printID();
}
public static void main(String[] args) {
Teacher ob = new Teacher();
ob.printID();
}
}
```
**Output:**
```plaintext
Teacher ID: 10
School ID: 1
```
**Explanation:**
* We can't directly use the `School' class's' printID()` method in the `Teacher` class.
* This is because the `printID()` method in the `Teacher` class overrides the one in the parent class.
* To access it, we need to use the `super` keyword, as shown earlier.
### To Invoke Immediate Parent Class Constructor
The `super` keyword can invoke both the default and parameterized constructors of the parent class in the child class. This is useful when we want to initialize the instance members of the parent class from the child class.
**Example - 1**
```java
// Base class Person
class Person {
Person() {
System.out.println("Person class Constructor");
}
}
// Derived class Teacher
class Teacher extends Person {
Teacher() {
// Invoke the parent class constructor
super();
System.out.println("Teacher class Constructor");
}
}
class Main {
public static void main(String[] args) {
// Invoke the constructor of the Teacher class
Teacher s = new Teacher();
}
}
```
**Output:**
```plaintext
Person class Constructor
Teacher class Constructor
```
**Explanation:**
* In the example above, the `Teacher` class extends the `Person` class.
* We use `super()` to invoke the parent class's constructor.
**Example - 2**
In Java, we can call the parameterized constructor of the parent class from the subclass using the Super Keyword. Let's understand this using an example.
```java
class Birds {
int age;
Birds(int age) {
this.age = age;
System.out.println("Birds class constructor invoked!");
}
}
public class Parrot extends Birds {
String name;
Parrot(String name, int age) {
super(age);
this.name = name;
System.out.println("Parrot class constructor invoked!");
}
void printDetails() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
public static void main(String[] args) {
Parrot ob = new Parrot("Teepee", 5);
ob.printDetails();
}
}
```
**Output:**
```plaintext
Birds class constructor invoked!
Parrot class constructor invoked!
Name: Teepee
Age: 5
```
:::
:::section{.main}
## Real Use Example of Java super Keyword
The `super()` keyword calls a parameterized constructor of the superclass from the subclass constructor. It initializes superclass members before executing the subclass's code.
**Example:**
```java
class Parent {
Parent(int value) {
System.out.println("Parent constructor with value: " + value);
}
}
class Child extends Parent {
Child() {
super(10); // Calling superclass constructor with value 10
System.out.println("Child constructor");
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
}
}
```
**Output:**
```plaintext
Parent constructor with value: 10
Child constructor
```
:::
:::section{.main}
## Advantages of Using Java super Keyword
Advantages of using the Java "super" keyword:
1. **Access to Superclass Members:** `super` enables access to the superclass's methods, variables, and constructors, promoting code reuse and leveraging existing functionality.
2. **Method Overriding Support:** `super` allows explicit invocation of overridden methods in the superclass, extending behaviour while retaining the original implementation.
3. **Constructor Chaining:** `super` facilitates constructor chaining, enabling subclasses to invoke constructors of their superclass for proper initialization and reducing code duplication.
4. **Flexibility in Inheritance:** `super` permits navigation through multiple levels of inheritance, accessing members and constructors of any superclass in the hierarchy, providing flexibility and control over the inheritance structure.
### Important Points to Remember While Using Java super Keyword
1. The subclass constructor's `super()` call must be the first statement to ensure proper initialization and execution order between the superclass and subclass.
2. If a constructor doesn't explicitly call a superclass constructor, the Java compiler automatically inserts a call to the superclass's no-argument constructor.
3. Constructor chaining occurs when a subclass constructor calls a constructor of its superclass, establishing a chain of constructor invocations up to the `Object` class.
:::
:::section{.summary}
## Conclusion
* The `super` keyword in Java is used to access the members of the immediate parent class.
* The Java compiler implicitly calls the no-arg constructor of the parent class in case a call to the `super()` is not made in the child class's constructor.
* We don't need the `super` keyword to access non-overridden members and methods of the parent class.
:::
:::section{.faq-section}
## FAQs
Q. **What is super() and super keyword in Java?**
A. The super() keyword in Java invokes the superclass's constructor within a subclass. It is used to access and initialize the superclass's members.
Q: **What is the super constructor in Java?**
A. The super constructor in Java is the constructor of the superclass. It is called using the super() keyword from within the constructor of a subclass to initialize the inherited members of the superclass.
Q: **Can we have this() and super() together?**
A. No, it is not possible to use both this() and super() together in Java. They both refer to constructor calls and cannot be used simultaneously.
:::