Newer Version Available

This content describes an older version of this product. View Latest

Using the super Keyword

The super keyword can be used by classes that are extended from virtual or abstract classes. By using super, you can override constructors and methods from the parent class.

For example, if you have the following virtual class:
You can create the following class that extends Superclass and overrides its printName method:

The expected output when calling Subclass.printName is My name is Mr. Vonderburg. But you can call me Carl.

You can also use super to call constructors. Add the following constructor to SubClass:

Now, the expected output of Subclass.printName is My name is Madam Clapentrap. But you can call me Brenda.

Best Practices for Using the super Keyword

  • Only classes that are extending from virtual or abstract classes can use super.
  • You can only use super in methods that are designated with the override keyword.