Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

super Statement

Refers to the base object of the current object. This can be used in two contexts.

// Syntax 1: Calls the base-class constructor with arguments.
super(arguments)

// Syntax 2: Accesses a member of the base class.
super.member
arguments

Optional in syntax 1. A comma delimited list of arguments for the base-class constructor.

member

Required in syntax 2. Member of the base class to access.

The super keyword is typically used in one of two situations. You can use it to explicitly call the base-class constructor with one or more arguments. You can also use it to access base-class members that have been overridden by the current class.

In the following example, super refers to the constructor of the base class.

class baseClass {
   function baseClass() {
      print("Base class constructor with no parameters.");
   }
   function baseClass(i : int) {
      print("Base class constructor. i is "+i);
   }
}
class derivedClass extends baseClass {
   function derivedClass() {
      // The super constructor with no arguments is implicitly called here.
      print("This is the derived class constructor.");
   }
   function derivedClass(i : int) {
      super(i);
      print("This is the derived class constructor.");
   }
}

new derivedClass;
new derivedClass(42);

This program displays the following output when run.

Base class constructor with no parameters.
This is the derived class constructor.
Base class constructor. i is 42
This is the derived class constructor.

In the following example, super allows access to an overridden member of the base class.

class baseClass {
   function test() {
      print("This is the base class test.");
   }
}
class derivedClass extends baseClass {
   function test() {
      print("This is the derived class test.");
      super.test(); // Call the base class test.
   }
}

var obj : derivedClass = new derivedClass;
obj.test();

This program displays the following output when run.

This is the derived class test.
This is the base class test.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.