Open In App

Difference between super and super() in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

In java it is predefined that the ‘super’ word is somewhere related to the parent class. If we need to brief and justify the title in one go then the super keyword in java refers to dealing with parent class object while super() deals with parent class constructor. We will be covering the article first discussing the concept of a super keyword followed by super() later onwards.

Concept: super keyword

The super keyword in java is a reference variable that is used to refer parent class objects. The keyword “super” came into the picture with the concept of Inheritance. Basically this form of super is used to initialize superclass variables when there is no constructor present in superclass. On the other hand, it is generally used to access the specific variable of a superclass.

Example

Java




// Java Program to Illustrate super keyword
 
// Class 1
// Base class
// Here it is vehicle class
class Vehicle {
 
    // Attribute
    int maxSpeed = 120;
}
 
// Class 2
// sub class Car extending vehicle
class Car extends Vehicle {
    int maxSpeed = 180;
 
    // Method
    void display()
    {
        // Printing maxSpeed of parent class (vehicle) as
        // super keyword refers to parent class
        System.out.println("Maximum Speed: "
                           + super.maxSpeed);
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of child class
        Car small = new Car();
 
        // Calling out method defined inside child class
        small.display();
    }
}


Output

Maximum Speed: 120

Now we already have seen that super keyword refers to parent class which can be perceived from the output itself. Now let us dwell onto second concept known as super()  which more programmers are lesser aware and do not implement same barely knowing it all  

Concept: super()

The super keyword can also be used to access the parent class constructor by adding ‘()’ after it, i.e. super(). Also do remember that ‘super()’ can call both parametric as well as non-parametric constructors depending upon the situation.

Example: 

Java




// Java code to demonstrate super()
 
// Class 1
// Helper class
// Parent class - Superclass
class Person {
 
    // Constructor of superclass
    Person()
    {
        // Print statement of this class
        System.out.println("Person class Constructor");
    }
}
 
// Class 2
// Helper class
// Subclass extending the above  superclass
class Student extends Person {
    Student()
    {
        // Invoking the parent class constructor
        // with the usage of super() word
        super();
 
        // Print styatement whenever subclass constructor is
        // called
        System.out.println("Student class Constructor");
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of subclass
        // inside main() method
        Student s = new Student();
    }
}


Output: 

Person class Constructor
Student class Constructor

 

Finally after having an adequate understanding of the above topics let us do finally conclude out differences between them which are listed in the tabular format below as follows:

super super()
The super keyword in Java is a reference variable that is used to refer parent class objects. The super() in Java is a reference variable that is used to refer parent class constructors.
super can be used to call parent class’ variables and methods. super() can be used to call parent class’ constructors only.
The variables and methods to be called through super keyword can be done at any time, Call to super() must be first statement in Derived(Student) Class constructor.
If one does not explicitly invoke a superclass variables or methods, by using super keyword, then nothing happens If a constructor does not explicitly invoke a superclass constructor by using super(), the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

 



Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads