Understanding Keyword Super: Java Tutorial

super javaObject-oriented languages consist of three integral features: inheritance, polymorphism and data encapsulation. Inheritance refers to the process of inheriting characteristics of parent object in the child object without having to rewrite the code in the child object. Polymorphism refers to providing a unified interface for achieving multiple functionalities through method overloading and method overriding. Encapsulation refers to data hiding that results in managed access to the data using methods. Java is one of those object-oriented languages that offers the three aforementioned features. Java contains several built-in keywords that can be used to communicate between parent and child classes. One such keyword is the “super” keyword.

Want to learn more about Java? Take a course at Udemy.com.

What is Super keyword in Java?

Often times, a child class need to interact with a parent class and methods. Sometimes, the instance variables of the parent class are required in a child class instance. In such scenarios, the super keyword can be used, which provides access to the parent class variables and methods using a child class instance.

Uses of Super in Java

In Java, a super keyword basically has three uses. These uses are explained in the next section in detail.

1-     Super for accessing parent class member variables.

Consider a scenario where a there is a parent class Shape and a child class Square. The parent class Shape has an integer type variable named, ”area”. The Shape class also contains a member variable named “area”. If the area member of the Square class is accessed, the area instance of the Square class will be accessed. This is best explained with the help of an example:

Consider a parent Shape class with following class definition:

public class Shape {
public int area = 45;
}
A Square class that extends the Shape class looks like this:
public class Square extends Shape
{
public int area=10;
public int getArea()
{
return area;
}
}

Now, if the getArea method is called from the instance of the Square class, the returned area will be the value of the area instance of the Square class. However, the parent class Shape also has the area as member variable. This is demonstrated in the following code snippet:

Square s = new Square();
System.out.println(s.getArea());

The above code would display 10 on the console screen, which is the area of the child class Square.

But what if, the area of the parent class Shape is needed? How can you get the value of that area instance? The child class also contains an instance member with the same name. In such scenarios, the super keyword is used. In the following example, slight changes have been made in the Square class of the last example. Have a look at the modified definition of the Square class.:

public class Square extends Shape
{
public intarea=10;
public int getArea()
{
return area;
}
public int getParentArea()
{
return super.area;
}
}

In the above code snippet, the Square class contains an additional method named getParentArea. Inside the method, the super keyword has been used followed by the dot operator and the instance member of the parent class to be accessed that is the area in this case. This value has been returned to calling function.

Now, executing following line of code will display 45, which is the area of the Parent class Shape.

System.out.println(s.getParentArea());

You can see that though ‘s’ is the object of the Square class, the parent class’ area is accessed through this object.

Basically, the super keyword serves as reference to the parent class object in the child class. Therefore, when the keyword super is used inside the child Square, the super keyword would serve as the reference to Shape class instance.

To learn more about Java, check out Udemy.com courses

2-     Super for Accessing Parent class constructor

The super keyword can be used to access the parent class constructor inside the child class. To see how the super keyword can be used to access the parent class constructor, have a look at the following example:

Modify the Shape class in the last example to include a class. The Shape class should now look like this:

public class Shape {
Shape()
{
System.out.println("This is parent Shape class constructor");
}
}

Also, change the definition of the Square class and include a constructor in the Square class too. The Square class should now look like this:

public class Square extends Shape
{
Square()
{
super();
System.out.println("This is child Square class constructor");
}
}

In the above code, inside the constructor of the Square class, the super keyword is used as the method “super()”. The usage of the keyword super as a method would invoke the parent class’s constructor.

Now, if the object of the Square class is created, the constructor of the Square class would be called. Inside the constructor of the Square class, first the constructor of Shape class will be called via super() and then the rest of the code inside the Square class constructor would be displayed.

Now, If the Square class is instantiated as follows:

Square s = new Square();

The console output would look like this:

This is parent Shape class constructor

This is child Square class constructor

3-     Super for accessing parent class methods

The third and final usage of the super keyword is for accessing the parent class methods in the child class. Modify the Shape and Square classes as follows to comprehend this concept.

Shape Class:

public class Shape {
int area = 45;
public void displayArea()
{
System.out.println("The area of parent class Shape is: "+area);
}
}

Square Class:
public class Square extends Shape
{
int area = 10;
public void displayArea()
{
super.displayArea();
System.out.println("The area of child class Square is: "+area);
}
}

In the Square class, inside the displayArea() method, the displayArea() method of the parent class has been called using the super.displayArea() method. Now, if the displayArea() method is called on the instance of the Square class, it will first call the displayArea() method of the parent Shape class and then the remaining code in the displayArea() method of the child Square class is executed. The following code demonstrates this concept:

Square s = new Square();
s.displayArea();

The output of the above code will be:

The area of parent class Shape is: 45

The area of child class Square is: 10

To learn essentials of the Java language, see online classes at Udemy.com