Super Keyword in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

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.

Usage of Java super Keyword

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:

Output:

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:

Output:

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

Output:

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.

Output:

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:

Output:

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.

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.

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.