Super keyword use with examples


super keyword performs important role in three places

  • Constructor Level
  • Method Level
  • Variable Level

Constructor Level

super keyword is used to call parent class constructor. This constructor can be default constructor or parameterized constructor.

  • Default constructor : super();
  • Parameterized constructor : super(int no, double amount, String name);
  • class FirstClass {
        FirstClass() {
            System.out.println("Constructor of FirstClass");
        }
    }
     
    class SecondClass extends FirstClass {
        SecondClass() {
            /* Compiler adds super() here at the first line
             * of this constructor implicitly
             */
            System.out.println("Constructor of SecondClass");
        }
     
        SecondClass(int num) {
            /* Compiler adds super() here at the first line
             * of this constructor implicitly
             */
            System.out.println("Constructor with argument");
        }
     
        void printMessage() {
            System.out.println("Hello");
        }
     
        public static void main(String args[]) {
            // Creating object using default constructor
            SecondClass obj = new SecondClass();
            // Calling subclass method
            obj.printMessage();
            // Creating object 2 using argument constructor
            SecondClass obj2 = new SecondClass(5);
            obj2.printMessage();
        }
    }

Note : super() must be the first statement in constructor otherwise we will get the compilation error message.

Method Level

super keyword can also be used in case of method overriding. super keyword can be used to invoke or call parent class method.

class BaseClass {
    // Overridden method
    void showMessage() {
        System.out.println("Base class method");
    }
}
 
class DerivedClass extends BaseClass {
    // Overriding method
    void showMessage() {
        System.out.println("Derived class method");
    }
 
    void printMessage() {
        // This would call Overriding method
        showMessage();
        // This would call Overridden method from the base class
        super.showMessage();
    }
 
    public static void main(String args[]) {
        DerivedClass obj = new DerivedClass();
        obj.printMessage();
    }
}

Note : If there is not method overriding then we do not need to use super keyword to call parent class method.

Variable Level

super is used to refer immediate parent class instance variable. In case of inheritance, there may be possibility of base class and derived class may have similar data members.In order to differentiate between the data member of base/parent class and derived/child class, in the context of derived class the base class data members must be preceded by super keyword.

// Base class or Superclass
class MyBaseClass {
    int baseNum = 200;
}
 
// Derived class or subclass
class MyDerivedClass extends MyBaseClass {
    // I am declaring the same variable
    // baseNum in the child class too.
    int derivedNum = 220;
 
    void printNumbers() {
        System.out.println(derivedNum);    // It will print value 220
        System.out.println(super.baseNum); // It will print value 200
    }
 
    public static void main(String args[]) {
        MyDerivedClass myObj = new MyDerivedClass();
        myObj.printNumbers();
    }
}

Note : If we are not writing super keyword before the base class data member name then it will be referred as current class data member and base class data member are hidden in the context of derived class.

Basic Programs