Core Java

Java Super Keyword Example

In this post, we feature a comprehensive article about the Java Super Keyword.

1. Introduction

super keyword is a reference variable that is used to refer to the object of the immediate parent class.

To understand what is immediate parent, consider an example where Class B extends A and class C extends B, in this case, class A is immidate parent for class B and class B is an immediate parent for class C.

2. Usage of the Java super Keyword

There is multiple usages of the super keyword.

  1. To call the immediate parent class variables.
  2. To call the immediate parent class methods.
  3. To call the immediate parent class constructor.

Let’s see the examples for each usage:

2.1 To call the immediate parent class variables

SuperWithVariables.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
public class SuperWithVariables {
 
    public static void main(String[] args) {
        Bike bike = new Bike();
        bike.printWheels();
    }
 
}
 
class Vehicle {
    int noOfWheels = 4;
}
 
class Bike extends Vehicle {
    int noOfWheels = 2;
 
    public void printWheels() {
        System.out.println("Number of wheels in Bike : " + noOfWheels);
        System.out.println("Number of wheels in Vehicle : " + super.noOfWheels);
    }
}

Output

1
2
Number of wheels in Bike : 2
Number of wheels in Vehicle : 4

In this example both Vehicle and Bike have the same name class variable, so when we call noOfWheelsin Bike class, the variable from Bike will be called so we need some mechanism to differentiate the variables from a parent class and child class. In order to call the parent class variable we use super keyword followed by a . and the variable name in the child class.

2.2 To call the immediate parent class methods

SuperWithMethods.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
public class SuperWithMethods {
 
    public static void main(String[] args) {
        Apple apple = new Apple();
        apple.fruitColor();
    }
 
}
 
class Fruit {
    public void fruitColor() {
        System.out.println("The fruit has a color.");
    }
}
 
class Apple extends Fruit {
    public void fruitColor() {
        super.fruitColor();
        System.out.println("Apple is Red.");
    }
}

Output

1
2
The fruit has a color.
Apple is Red.

The above example demonstrate the usage of super keyword for calling parent class method. Here in this example we override the fruitColor() method of Fruit class in Apple class. If we simply call fruitColor() inside Apple class, overriden method will be called. In order to use parent class method we use super keyword followed by a . and the method name in the child class. 

2.3 To call the immediate parent class constructor

SuperWithConstructors.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class SuperWithConstructors {
    public static void main(String[] args) {
        new Child();
    }
}
 
class Child extends Parent {
 
    public Child() {
        System.out.println("Inside Child constructor");
    }
 
}
 
class Parent extends SuperClass {
 
    public Parent() {
        this("Hello World");
        System.out.println("Inside Parent constructor");
    }
 
    public Parent(String s) {
        System.out.println("Inside Parent parameterized constructor with value : " + s);
    }
 
}
 
class SuperClass {
 
    public SuperClass() {
        System.out.println("Inside SuperClass constructor");
    }
 
}

Output

1
2
3
4
Inside SuperClass constructor
Inside Parent parameterized constructor with value : Hello World
Inside Parent constructor
Inside Child constructor

In the above example, we have three classes called SuperClass which is extended by Parent, which is further extended by Child class. When we create an instance of Child class, the Parent class no-argument constructor is automatically called. In this example when we create the instance of Child class then constructor call goes all the way to the root class and then its child constructors are called in order.

Note:

  • While using the super() to call the parent constrcutor, it should be the first statement in the child constructor.
  • If we dont call super() in child class, compiler automatically call no argument constructor of parent class. If there is only parameterized constructor in parent class then compiler is unable to call automatically and explicitly constructor call is required.  

3. Download the source code

Download
You can download the full source code of this example here: Java Super Keyword Example

Yusuf Ansari

I am Yusuf Ansari, I earned my degree in Information Technology from Guru Gobind Singh Indraprastha University. I have over two years of professional experience in Java based application development.I have worked as a backend developer mostly but also worked on the front-end part in some React.js based projects.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button