- Using the Keyword super
- Subclass Constructors
- Super Keyword in Java
- Usage of Java super Keyword in Java
- Refer immediate parent class instance variable
- Invoke immediate parent class method
- Invoke immediate parent class constructor
- Example — 1
- Example — 2
- More Examples
- Call Parameterized Constructor Using super()
- super() is Provided by the Compiler Implicitly
- If the Child Class is Not Overriding Any Method
- Advantages of Using Super Keyword in Java
- Important Points
- FAQs
- Conclusion
- Related Topics
- What is the super() construct of a constructor in Java?
- Example
- Output
- What is the super() construct of a constructor in Java?
- Example
- Output
Using the Keyword super
If your method overrides one of its superclass’s methods, you can invoke the overridden method through the use of the keyword super . You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass :
Here is a subclass, called Subclass , that overrides printMethod() :
public class Subclass extends Superclass < // overrides printMethod in Superclass public void printMethod() < super.printMethod(); System.out.println("Printed in Subclass"); >public static void main(String[] args) < Subclass s = new Subclass(); s.printMethod(); >>
Within Subclass , the simple name printMethod() refers to the one declared in Subclass , which overrides the one in Superclass . So, to refer to printMethod() inherited from Superclass , Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:
Printed in Superclass. Printed in Subclass
Subclass Constructors
The following example illustrates how to use the super keyword to invoke a superclass’s constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle . Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own:
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear)
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
With super() , the superclass no-argument constructor is called. With super(parameter list) , the superclass constructor with a matching parameter list is called.
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object . In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
Super Keyword in Java
The Java super keyword is used as a reference variable to access objects from parent classes, allowing access to parent class constructors, members, and methods in the derived class. It plays a crucial role in inheritance and polymorphism concepts.
Usage of Java super Keyword in Java
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, which is School .
Explanation:
- Two classes are created: School and Teacher , with Teacher extending School .
- Both classes have id and name variables.
- The super keyword is used to reference 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 .
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.
Explanation:
- We can’t directly use the printID() method of the School class 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.
Invoke immediate parent class constructor
The super keyword can be used to 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
Explanation:
- In the example above, the Teacher class extends the Person class.
- We use super() to invoke the constructor of the parent class.
Example — 2
We can call the parameterized constructor of the parent class from the subclass using the Super Keyword in Java. Let’s understand this using an example.
More Examples
Call Parameterized Constructor Using super()
The super() keyword calls a parameterized constructor of the superclass from the subclass constructor. It initializes superclass members before executing the subclass’s code.
super() is Provided by the Compiler Implicitly
If a subclass constructor doesn’t explicitly invoke a superclass constructor using super() , the Java compiler automatically inserts a call to the no-argument constructor of the superclass. This guarantees proper initialization of the superclass.
If the Child Class is Not Overriding Any Method
In this code snippet, since the child class does not override any method of the superclass, the super() call is not necessary in the constructor. The child class inherits and directly uses the methods from the parent class.
Advantages of Using Super Keyword in Java
Advantages of using the Java «super» keyword:
- Access to Superclass Members: super enables access to methods, variables, and constructors of the superclass, promoting code reuse and leveraging existing functionality.
- Method Overriding Support: super allows explicit invocation of overridden methods in the superclass, extending behavior while retaining the original implementation.
- Constructor Chaining: super facilitates constructor chaining, enabling subclasses to invoke constructors of their superclass for proper initialization and reducing code duplication.
- 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
- The super() call in the subclass constructor must be the first statement to ensure proper initialization and execution order between the superclass and subclass.
- If a constructor doesn’t explicitly call a superclass constructor, the Java compiler automatically inserts a call to the superclass’s no-argument constructor.
- Constructor chaining occurs when a subclass constructor calls a constructor of its superclass, establishing a chain of constructor invocations up to the Object class.
FAQs
Q: What is super() and super keyword in Java?
A. In Java, the super() keyword is used to invoke the superclass’s constructor within a subclass. It is used to access and initialize the members of the superclass.
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.
Conclusion
- The super keyword in Java is used to access the members of the immediate parent class.
- There are three main uses of the super keyword in Java:
- To access the data members of the immediate parent class
- To invoke the overridden methods of the immediate parent class
- To call both default and parameterized constructors 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 constructor of the child class.
- We don’t need the super keyword to access non-overridden members and methods of the parent class.
Related Topics
What is the super() construct of a constructor in Java?
The super keyword is similar to this keyword. Following are the scenarios where a super keyword is used.
- It is used to differentiate the members of superclass from the members of the subclass if they have same names.
- It is used to invoke the superclass constructor from the subclass.
Whenever you want to call the constructor of the superclass from a method or another constructor you can do so as:
Example
class Person < Person(String name) < System.out.println("Hello "+ name); >> class Student extends Person < Student(String name) < super(name); >public static void main(String args[]) < Student st = new Student("Ram"); >>
Output
An investment in knowledge pays the best interest
- Related Articles
- Using the super Keyword to Call a Base Class Constructor in Java
- What happens if we call «super()» in a constructor without extending any class, in java?
- Super constructor in Dart Programming
- Can you use both this() and super() in a constructor in Java?
- What is the purpose of a constructor in java?
- What is a constructor in Java?
- What is the use of constructor in Java?
- What is the purpose of a default constructor in Java?
- What is the return type of a Constructor in Java?
- What is the super class of every class in Java?
- What is the purpose of private constructor in Java?
- What is the use of parametrized constructor in Java?
- Java Program to Allocate and Initialize Super Class Members using Constructor
- What is constructor overloading in Java?
- What is constructor chaining in Java?
What is the super() construct of a constructor in Java?
The super keyword is similar to this keyword. Following are the scenarios where a super keyword is used.
- It is used to differentiate the members of superclass from the members of the subclass if they have same names.
- It is used to invoke the superclass constructor from the subclass.
Whenever you want to call the constructor of the superclass from a method or another constructor you can do so as:
Example
class Person < Person(String name) < System.out.println("Hello "+ name); >> class Student extends Person < Student(String name) < super(name); >public static void main(String args[]) < Student st = new Student("Ram"); >>
Output
An investment in knowledge pays the best interest
- Related Articles
- Using the super Keyword to Call a Base Class Constructor in Java
- What happens if we call «super()» in a constructor without extending any class, in java?
- Super constructor in Dart Programming
- Can you use both this() and super() in a constructor in Java?
- What is the purpose of a constructor in java?
- What is a constructor in Java?
- What is the use of constructor in Java?
- What is the purpose of a default constructor in Java?
- What is the return type of a Constructor in Java?
- What is the super class of every class in Java?
- What is the purpose of private constructor in Java?
- What is the use of parametrized constructor in Java?
- Java Program to Allocate and Initialize Super Class Members using Constructor
- What is constructor overloading in Java?
- What is constructor chaining in Java?