Super Keyword in Java

super-keyword-in-java

Super Keyword in Java

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. It is majorly used in the following contexts:

1. Use of super with variables: 

This situation happens when an inferred class and base class has same information individuals. All things considered there is a chance of vagueness for the JVM. We can comprehend it all the more obviously utilizing this code bit:

/* 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

In the above example, both base class and subclass have a member maxSpeed. We could access maxSpeed of base class in subclass using super keyword.

2. Use of super with methods: This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the said usage of super keyword.

/* Base class Person */
class Person
{
void message()
{
System.out.println(“This is person class”);
}
}

/* Subclass Student */
class Student extends Person
{
void message()
{
System.out.println(“This is student class”);
}

// Note that display() is only in Student class
void display()
{
// will invoke or call current class message() method
message();

// will invoke or call parent class message() method
super.message();
}
}

/* Driver program to test */
class Test
{
public static void main(String args[])
{
Student s = new Student();

// calling display() of Student
s.display();
}
}

Output:

This is student class
This is person class

In the above example, we have seen that if we only call method message() then, the current class message() is invoked but with the use of super keyword, message() of superclass could also be invoked.

 

3Use of super with constructors: 

super keyword can likewise be utilized to get to the parent class constructor. One more significant thing is that, ”super’ can call both parametric just as non parametric constructors relying on the circumstance. Following is the code piece to clarify the above idea:

/* 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

In the above example we have called the superclass constructor using keyword ‘super’ via subclass constructor.

Other Important points:

  1. Call to super() must be first statement in Derived(Student) Class constructor.
  2. 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 superclass 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.
  3. If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that a whole chain of constructors called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining..