Super Class in Java

by | Jan 6, 2023 | Java

What is Super Class in Java?

Super classes and sub classes are a major concept of inheritance in Java. ‘super’ is treated as a keyword in Java. Any variable called with a ‘.super’ after the variable name refers to the variable of the same, not of the current class it is in, but of its super class. It is used a reference variable that is used to point to parent class objects. The super keyword does not really hold much significance if it is segregated from the concept of inheritance. It is used in the following cases:

Use of Super with variables

Using the concept of inheritance, Java allows developers to create super and sub classes. As evident from the name, sub classes derive features from the super class while having features of its own. Problems arise when both the super and the sub classes have variables with the same name. In such cases, when you reference a variable, the compiler gets confused as to which variable of which class is being referred to. To make the workflow of the program easy for the compiler, when referring to super class variables, we generally use ‘super.’ before calling the variable. The phenomenon is better illustrated from the code below:

class Vehicle
{
int maxS = 120;
}
class Car extends Vehicle
{
int maxS = 180;
void display ()
{
System.out.println (“Maximum Speed of the car is ” + super.maxS);
}
}
class test
{
public static void main (String args [])
{
Car small = new Car ();
small.display ();
}
}

 

OUTPUT:

Maximum Speed is: 120

In the example given above, we notice that both the super as well as the sub class had the maxS variable. However, using the super keyword, we could get the super class variant of the variable.

Use of super with methods

As we have seen with the variables above, methods also work in a similar fashion. Sub classes can inherit methods of the superclass while having methods of its own. Therefore, it is imperative that we specifically notify the JVM which method we want to call when methods of the sub class and the super class have the same name. With the help of the super keyword, we can call super class methods from a sub class body. The following code sheds light on the problem:

class Person
{
void message ()
{
System.out.println (“This is person Class”);
}
}
class Student extends Person
{
void message ()
{
System.out.println (“This is the student class”);
}
void display ()
{
message ();
super.message();
}
}
class Test
{
public static void main (String args [])
{
Student s = new Student ();
s.display ();
}
}

 

OUTPUT:

This is a student class
This is a person class

From the code above, we find an interesting property of Java. When we simply call the method message() without any keyword, only the current message method, that is the method of the sub class is called. However, when we use the super keyword along with it, we can refer to the message method of the super class.

Use of super constructors

The super keyword also plays a crucial rule in accessing the constructor of the super class from inside the constructor block of any sub class. Also, super can call both parametric and non-parametric constructors depending upon the situation. The following code illustrates the use of super in constructors better:

class Person
{
Person ()
{
System.out.println (“This is person Class”);
}
}
class Student extends Person
{
Student ()
{
super();
System.out.println (“This is the student class”);
}
}
class Test
{
public static void main (String args [])
{
Student s = new Student ();
}
}

 

OUTPUT:

This is a person class
This is a student class

In the program above, we have called the super class constructor using the super keyword from within the constructor block of the sub class.

Other important points to be noted are:

  1. It is a mandatory rule that call to the super class constructor via the super keyword should be the first line in the sub class constructor. This follows the logic that the super class constructor should be initialized before the sub class as the sub class is a derived version of the super class.
  2. Suppose a constructor of a sub class does not call the constructor of the super class using the super keyword. In that case, the Java compiler automatically calls the super class constructor via a no-argument call. If the super class has some constructor parameters, you will then get a compile time error namely Object does not have a constructor. Therefore, if the object is the only super class, there is no problem.
  3. A whole string of constructors is called, all the way back to the constructor of the object if a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly. Java gives a typical name for this kind of operation called constructor chaining.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.