IntelliJ IDEA 2018.1 Help

Override Methods of a Superclass

You can override any method of a parent class by generating necessary code from a predefined template. IntelliJ IDEA creates a stub that contains a call to the method of the superclass, leaving the developer with the task of providing some meaningful source code in the method's body.

To override a method of a superclass:

  1. On the Code menu, click Override methods (Ctrl+O). Alternatively, you can right-click anywhere in the class file, then click Generate (Alt+Insert), and select Override methods.
  2. Select the methods to override. The list of methods does not include the methods that are already overridden, or cannot be accessed from the current subclass.
    ij overrideMethod

    If necessary, select the Copy JavaDoc check box to insert JavaDoc comments for the overridden methods.

    If necessary, select the Insert @Override check box to insert the @Override annotation.

  3. Click OK and provide the source code for the method body.

    Use the gutterIconOverriding icon that marks the overriding method in the left gutter to view the name of the base class, and to open the overridden method declaration.


Change method body

The code template used for overriding methods (Overridden method body) accepts predefined template variables from the File Header include template (such as, ${USER}, ${DATE}, etc.)

For example, consider the following code template:

#if ( $RETURN_TYPE != "void" )return $DEFAULT_RETURN_VALUE;#end // TODO ($USER, $DATE):To change the body of an implemented method, use File | Settings - Editor - File and Code Templates.

Provided that the overridden class contains two methods, this template expands into the following code:

public void breathe() { // TODO (wombat, 9/21/12): To change the method body, use Settings - Editor - File and Code Templates. } public void eat() { // TODO (wombat, 9/21/12): To change the method body, use Settings - Editor - File and Code Templates. }
Last modified: 24 July 2018