Java Super Keyword
The super keyword in Java refers to the parent class of an object. The parent class is known as the Superclass.
The super keyword helps the Subclass access the Superclass's components, such as constructors, variables and methods.
Super keyword is vital in implementing OOP (Object-oriented programming) principles such as inheritance and polymorphism, and it is better to know about this concept before understanding super in Java.
Usage of Java Super keyword
The super keyword is used to refer to or invoke the following component:
- Parent class variable
- Parent class method
- Parent class constructor
1) Super keyword to refer to the parent class variable
In this case, the Superclass and Subclass have the same data variable. Thus, we can access the variable of the Superclass using the super keyword.
FileName: SuperExample1.java
// Parent or Superclass class University{ // instance variable in Superclass int id; int students = 2000; } //Subclass which extends Superclass class Department extends University{ // instance variable in Subclass int id; int students = 300; void noOfStudents(){ //prints noOfStudents of Department class System.out.println("The number of students is "+students); //prints noOfStudents of University class System.out.println("The number of students is " +super.students); } } public class SuperExample1{ public static void main(String args[]){ Department d=new Department(); // calling noOfStudents() of Department class d.noOfStudents(); }}
Output:
The number of students is 300 The number of students is 2000
Explanation:
In the program shown above, the University and Department classes have the same students variable. When we print the students variable without using any keyword, it will, by default, print the students from its current class. On the other hand, to print the students from the University class (Parent class), we can use the super keyword.
2) Super keyword to invoke the parent class method
In this case, both the Superclass and Subclass have the same method. Thus, we can invoke the method of the Superclass using the super keyword. In other words, we can utilize the super keyword to invoke the overridden methods.
FileName: SuperExample2.java
// Parent or Superclass class University{ int students = 2000; // method in the parent class void printName() { System.out.println("National Technological University"); } } //Subclass which extends Superclass class Department extends University{ int students = 300; // method in subclass void printName() { System.out.println("Information Technology"); } void noOfStudents() { System.out.println("The number of students are "+students); } void printInfo() { // invokes the method from the current class printName(); // invokes the method from the parent class super.printName(); noOfStudents(); } } public class SuperExample2 { public static void main(String args[]){ Department d=new Department(); // calling printInfo() of Department class d.printInfo(); }}
Output:
Information Technology National Technological University The number of students are 300
Explanation:
In the program shown above, the University and Department classes have the same method named printName(). The method is overridden. When we print the printName() method without using any keyword, it will, by default, call the method from its current class. On the other hand, to call the method from the University class (Parent class), we can use the super keyword.
3) Super keyword to invoke the parent class constructor
The super keyword can also be utilized to invoke the constructors of the Superclass and use it in the Subclass. Both the parameterized and non-parameterized constructors can be invoked in the Subclass.
Invoke Non – Parameterized constructor
In the program shown below, we have utilized the super keyword to invoke the parent class constructor in the child class constructor.
FileName: SuperExample3.java
// Parent or Superclass class University{ public University() { System.out.println("Constructor in University class"); } } //Subclass or child class which extends Superclass class Department extends University{ public Department() { // invoke the super or parent class constructor super(); System.out.println("Constructor in Department class"); } } public class SuperExample3 { public static void main(String args[]){ Department d = new Department(); }}
Output:
Constructor in University class Constructor in Department class
Invoke Parameterized Constructor
We can also invoke a parametrized constructor in the child class from the parent class utilizing the super keyword, similar to the previous example.
FileName: SuperExample4.java
// Parent or Superclass class Vehicle{ int speed; public Vehicle(int speed) { this.speed = speed; System.out.println("Constructor in the Vehicle class"); } } //Subclass which extends Superclass class Bike extends Vehicle{ String color; public Bike(int speed, String color) { // invokes the super class constructor super(speed); this.color = color; System.out.println("Constructor in the Bike class"); } void printInfo() { System.out.println("The Color of the Bike is " +this.color); System.out.println("The speed of the Bike is " +this.speed); } } public class SuperExample4 { public static void main(String args[]){ Bike bike = new Bike(80, "Black"); // calling the printInfo() method from subclass bike.printInfo(); }}
Output:
Constructor in the Vehicle class Constructor in the Bike class The Color of the Bike is Black The speed of the Bike is 80
Another Example
FileName: SuperExample5.java
// Parent or Superclass class Vehicle{ public Vehicle(int speed) { System.out.println("Constructor in parent class"); System.out.println(speed); } } //Subclass which extends Superclass class Bike extends Vehicle{ public Bike() { // invokes the superclass constructor super(90); System.out.println("Constructor in child class"); } } // main method public class SuperExample5 { public static void main(String args[]){ Bike bike = new Bike(); }}
Output:
Constructor in parent class 90 Constructor in child class
Note: super() should be the first statement in the child class constructor. This confirms that the Superclass has been set up correctly.
What if the child class doesn’t explicitly call the parent class constructor using the super keyword?
When the child class doesn't explicitly call the parent class constructor, the Java compiler automatically places the super () into the constructor.
FileName: SuperExample6.java
// Parent or Superclass class University{ public University() { System.out.println("Constructor in University class"); } } //Subclass or child class which extends Superclass class Department extends University{ public Department() { System.out.println("Constructor in Department class"); } } public class SuperExample6 { public static void main(String args[]){ Department d = new Department(); }}
Output:
Constructor in University class Constructor in Department class
Explanation:
In the program shown above, the Java compiler implicitly adds the super() in the child class constructor since it is not provided explicitly.
Benefits of Super Keyword
Super keyword offers various benefits in OOP (Object-oriented programming), and we have listed some of them below.
1) Code Reusability
With the help of the super keyword, we can utilize components of the parent class, such as variables, methods, and constructors. This allows us to reuse the code from the parent and child classes, reducing duplication and enabling code reusability.
2) Facilitate polymorphism
The methods from the parent class can be overridden in the child class by using the super keyword; thus, it enables polymorphism in programming.
3) Constructor chaining
When the child class's constructor calls the parent class's constructor, a whole chain of constructors is created. This chain formation of the constructor is known as constructor chaining.
4) Customize the behavior
The overridden methods in the Subclass can customize, extend and enhance the behavior of their Superclass.
5) Enhances code Readability
Using the super keyword, we can avoid continuous duplication of components and reuse them in subclasses, which enhances code readability. This improved code readability makes it easier to perform any changes or update the code in the future.
6) Enables Encapsulation and Abstraction
The super keyword enables OOP principles such as encapsulation and abstraction by permitting child classes to focus on their implementation while depending on the Superclass to manage lower-level details.
Points to Remember
- Using the super keyword in Java, we cannot refer to or invoke private components of the Superclass. The private components are available only within its class.
- Using the super keyword in the static method will give a compile-time error because the super keyword refers to an instance of the Superclass, which does not belong to the static context.
- The super() should be the firststatement in the child class constructor to ensure the proper order of execution between the parent class and the child class.
- The Java compiler automatically adds a no-argument constructor if the child class constructor does not explicitly call the parent class constructor. If the parent class contains a no-argument constructor, it will throw a compile-time error. There is no issue if the Object is the only parent class since the Object has such a constructor.
- this() and super() cannot be used simultaneously, as both refer to the constructor call.