Inheritance: Super Keyword Tutorial

In my Superclass Constructors Tutorial I discussed how a subclass constructor could invoke the constructor of a superclass by using the super keyword. We can use super in conjunction with the dot operator to access and invoke members of a superclass as well.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md SuperKeyword
C:\Java>cd SuperKeyword

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

class SuperKeyword {
    public static void main(String args[]) {
        CardboardBox b1 = new CardboardBox(5, 5, 5, "inches", "brown");
        CardboardBox b2 = new CardboardBox(14, 17, 13, "centimeters", "white");


class Box {
    int length = 0;
    int height = 0;
    int width = 0;
    String unitOfMeasurement = "";

    Box() {

    Box(int length, int height, int width, String unitOfMeasurement) {
        this.length = length;
        this.height = height;
        this.width = width;
        this.unitOfMeasurement = unitOfMeasurement;

    int returnVolume() {
        return (length * height * width);

class CardboardBox extends Box {     
    String color=""; 
    CardboardBox() {

    CardboardBox(int length, int height, int width, String unitOfMeasurement, String color ) {
        super(length, height, width, unitOfMeasurement);
        this.color = color;

    void displayMessage(String boxNumber) {
        System.out.println("Box #" + boxNumber + " is a " + this.color + " cardboard box with a volume of: " + super.returnVolume() + " cubic " + super.unitOfMeasurement);

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java SuperKeyword and press Enter.

C:\Java\SuperKeyword>java SuperKeyword
Box #1 is a brown cardboard box with a volume of: 125 cubic inches
Box #2 is a white cardboard box with a volume of: 3094 cubic centimeters

Final thoughts

The super keyword can be used in different ways to both access and invoke superclass members; you can use the super keyword to invoke constructors of a superclass as well.