Java: the “super” Keyword

By Xah Lee. Date: . Last updated: .

When creating a constructor of a class whose parent class has a user-defined constructor, then in the definition of the constructor it must call the parent class's constructor, in the first line, using the super keyword.

Following is a guide on why we need it.


Here's a example of inheritance of a class that has a constructor.

Note: constructors are never inherited, only variables and methods are. [see Java: Constructor]

// example of extending a class that has constructor

class B {
    B () {
        System.out.println("B called");
    }

    int ff (int n) {
        return n + 1;
    }
}

class C extends B {}

public class T {
    public static void main(String[] args) {
        C c = new C();
        System.out.println(c.ff(4)); // 5
    }
}

In the above code, C is subclass of B. So, C inherent B's methods. However, the special method B() is B's constructor, and it is not inherited because Java does not inherit constructors. In the “main” method, a instance of C is created, and the inherited method ff is called.

If you run the program, it will print “B called” followed by “5”.

In Java, when a subclass is created, it automatically calls its parent's constructor (if the class itself does not define constructors).

Extending a Class that has Constructor with Parameters

Here's a example of extending a class that has a constructor with parameters.

The code won't compile and gives a mysterious message. See if you can fix it.

/* note: this code won't compile*/

class B {
    B (int n) {
        System.out.println("B's constructor called");
    }
}

class C extends B {
    C (int n) {
        System.out.println("C's constructor called");
    }
}

public class I2 {
    public static void main(String[] args) {
        B b = new B(4);
        C c = new C(2);
     }
}

here's the error message. (from javac 1.8)

I2.java:10: error: constructor B in class B cannot be applied to given types;
    C (int n) {
              ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

The super is a instance of the parent class.

In our example, the class B has a user defined constructor. Therefore, Java did not create a do-nothing constructor for it. And, thus when we have a class C that extends B, you need to call the constructor of B.

To fix the code above, add super(n); in the first line of the subclass's constructor.

If the class C does not call B's constructor, Java compiler automatically assumes that the user wants the no-argument constructor B(), and call it for you. But since Java did not internally create the no-argument constructor, therefore it gives the error of symbol not found.

Here's the fixed code.

class B {
    B (int n) {
        System.out.println("B's constructor called");
    }
}

class C extends B {
    C (int n) {
        super(n);
        System.out.println("C's constructor called");
    }
}

public class I2 {
    public static void main(String[] args) {
        B b = new B(4);
        C c = new C(2);
    }
}

Java

  1. Install
  2. javac java jar javadoc

Java Basics

  1. Print String
  2. Strings
  3. Power Function
  4. Java Types and Type Conversion
  5. Function
  6. Arrays

Class, Inheritance

  1. extends
  2. Constructor
  3. Constructor Return Type
  4. Extending a Class that has Explicit Constructors
  5. static
  6. this
  7. super
  8. Access Specifiers

Abstract class, Interface

  1. abstract
  2. Interface

Package

  1. Package

Misc

  1. Collection, Map
  2. Read/Write File
  3. Convert File Encoding
  4. Complex Numbers in Java
  5. Unicode
  6. Number Literals