Skip to content
Related Articles

Related Articles

Difference between super and super() in Java with Examples
  • Difficulty Level : Easy
  • Last Updated : 05 Nov, 2019

super

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.




// Java code to demonstrate super keyword
  
/* Base class vehicle */
class Vehicle {
    int maxSpeed = 120;
}
  
/* sub class Car extending vehicle */
class Car extends Vehicle {
    int maxSpeed = 180;
  
    void display()
    {
        /* print maxSpeed of base class (vehicle) */
        System.out.println("Maximum Speed: "
                           + super.maxSpeed);
    }
}
  
/* Driver program to test */
class Test {
    public static void main(String[] args)
    {
        Car small = new Car();
        small.display();
    }
}
Output:
Maximum Speed: 120

super()

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






// Java code to demonstrate super()
  
/* superclass Person */
class Person {
    Person()
    {
        System.out.println("Person class Constructor");
    }
}
  
/* subclass Student extending the Person class */
class Student extends Person {
    Student()
    {
        // invoke or call parent class constructor
        super();
  
        System.out.println("Student class Constructor");
    }
}
  
/* Driver program to test*/
class Test {
    public static void main(String[] args)
    {
        Student s = new Student();
    }
}
Output:
Person class Constructor
Student class Constructor

Difference between super and super()

supersuper()
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 keywordd 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 happensIf 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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :