The super keyword is used in different situations. Before getting started with this post we encourage you to learn about inheritance in Java for a better understanding.
What is the super keyword in Java?
Super is a keyword that can be used to invoke overridden methods of the superclass, as well as to refer to hidden fields of the superclass.Why and when to use the super keyword?
The Java super keyword has three explicit uses.- Access the data members of the parent class when the child class also has data members with the same name.
- Call the default or parameterized constructors of the parent class in the child class.
- Call the parent class methods in the child class if the child has overridden methods.
Example 1 - Access parent’s data members
Example 1 illustrates how the attributes or data members of the Vehicle class can be accessed in the Car-type child class. Make sure you run the snippet below to understand well.
class Vehicle {
String name = "vehicle";
}
class Car extends Vehicle {
String name = "car";
public void printMyName() {
System.out.println(name);
}
public void printParentName() {
// use super keyword to access
// parent's data member / attribute
System.out.println(super.name);
}
public static void main(String[] args) {
Car myCar = new Car();
System.out.print("My Car's Name: ");
myCar.printMyName();
// printing the parent's name
// using the super keyword
System.out.print("My Parent Vehicle's Name: ");
myCar.printParentName();
}
}
Output
My Car's Name: car
My Parent Vehicle's Name: vehicle
Example 2 - Access the parent’s constructors in child class
Explicitly calling super() allows you to access both the default or parameterized constructor of the parent class in the child class. Here’s an example of the parameterized constructor. The parent i-e Shape class’ constructor is called (using super()) in the child i-e Triangle class to set the attributes. Run the program below to test the output for yourself.
public class Shape {
String name;
public Shape(String n) {
System.out.println("Shape() parameterized constructor called!");
name = n;
}
}
class Triangle extends Shape {
int sides = 3;
String color;
public Triangle(String n, String c) {
// The super keyword calls the parameterized
// constructor of the parent (Shape) with
// 'n' as a parameter
super(n);
System.out.println("Triangle() parameterized constructor called!");
this.color = c;
}
public static void main(String[] args) {
Triangle myTriangle = new Triangle("Triangle Alpha", "Yellow");
System.out.println(myTriangle.name);
System.out.println(myTriangle.color);
}
}
Output
Shape() parameterized constructor called!
Triangle() parameterized constructor called!
Triangle Alpha
Yellow
Quick Challenge: Redesign the above program by using the default constructor to test your learning. Also, see how super() is different from super(arg).
Example 3 - Access the parent’s overridden method in child class
Example 3 shows how you can access the methods of parent’s class that the child class also defines. The parent class Sound in the program below defines a method voice(). The child class Drum also has a method with the same name i-e voice(). It means the method voice is overridden by the subclass. Run the program below to learn how the super keyword is necessary to use the parent class’ methods in the child class.
public class Sound {
public void voice() {
System.out.println("Play sound!");
}
}
class Drum extends Sound {
public void voice() {
System.out.println("Play drums!");
}
public void play() {
// The super keyword calls the
// voice() method of the parent
super.voice();
voice();
}
public static void main(String[] args) {
Drum myDrum = new Drum();
myDrum.play();
}
}
Output
Shape() parameterized constructor called!
Triangle() parameterized constructor called!
Triangle Alpha
Yellow
GO TO FULL VERSION