Using super

Purpose:

To indicate that a subclass is calling a method that is defined in a superclass.

Mechanics:

super.<method name>();
super(); // the super class method has the same method name as the current method

Example:

super.animate();

Usage:

  • The super reserve word is used when a subclass's method wants to use a method defined in a superclass.
  • When Java encounters super, it searches for <method name> in the current class's immediate superclass. If the method is not found there, Java moves to the next superclass in the heirarchy. Java continues to move up the heirarchy until the method name is found and then calls that method.
  • super() is used only when <method name> has the same method name as the method in which the statement is found; for example foo wants to call the foo method defined in a superclass, super() should be used. It can be used for any method, though it is most commonly used for constructors.

Restrictions:

A call to the superclass constructor in a subclass constructor must occur in the first line of the subclass constructor.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu