Home

 

Adding method declarations to an interface

There are two ways to add a method to a class or interface: We add a Java method by using the Create Java Method wizard, or by writing directly the method declaration into the interface or class body in the Java editor. The Create Java Method wizard gets only called through the Visualizer Class Diagram editor:

Right-click the Bank interface in the diagram editor and select Add Java Æ Method, or just move the mouse pointer anywhere over the interface in the diagram editor, and click in the action bar above the class.

In the Create Java Method dialog, enter the following data (Figure | -24).

Name: searchCustomerBySsn
Visibility: public (default)
Modifiers: clear all (default)

Restriction: All methods of a Java interface are public abstract. Modifier abstract can be omitted, as it is per default abstract. Therefore, there is no choice by Visibility and Modifiers when you are adding a method declaration to an interface. An interface never has a constructor, so the constructor check box is never active.

Type: void (default)
Dimensions: 0 (default)
Throws: itso.rad75.bank.exception.InvalidCustomerException.

Add an exception, by clicking Add and typing the exception class name in the Pick one or more exception types to throw field. All matching types are listed in the Matching types field. Select the required exceptions and click OK.

Note that the InvalidCustomerException class must be created first under the package itso.rad75.bank.exception to be selected from the Browse Types list. Follow the instructions in Creating a Java class using the New Java Class wizard.

Parameters: java.lang.String ssn

Note: To add a parameter, click Add. Enter the name, select the type and dimensions in the Create Parameter dialog, and click OK to add the parameter. In the example, we do not pass any array parameters, and dimensions are always 0 (default).

Click Finish to create the Java method.

Figure 8-24 Create Java Method dialog

Note: There are two reasons why you might not see the methods in the class diagram:

The class diagram method compartment is collapsed-Select the interface or class and click the little blue arrow in the compartment in the bottom. That expands the method compartment.

The class diagram method compartment is filtered out-Right-click the interface or class and select Filters Æ Show/Hide Compartment Æ Method Compartment.

ITSOBank example-Interface methods

Repeat the foregoing steps to create the following method declarations to the Bank interface of the ITSO Bank application. Table | -6 lists all method declarations that can be created:

searchCustomerBySsn()-Type: itso.rad75.bank.model.Customer

getCustomers()-Type: java.util.Map

transfer()-Type: void

Notes:

Do not forget to set the correct parameters and exceptions to the methods that you create.

You can also import the final code later in Implementing the classes and methods.

ibm.com/redbooks