Home

 

Adding constructors and Java methods to a class

The way to add constructors and methods to a class is the same as when you add a method declaration to an interface. You can check the steps explained in Adding method declarations to an interface, except that there are no restrictions as described for the interfaces.

ITSOBank example-Class methods

Repeat the foregoing steps to create the following class methods for the ITSO Bank application. Be aware that you have to select Constructor when adding a constructor to a class. Table | -7 lists all method declarations that can be created:

Class: ITSOBank -Method: updateCustomer() and transfer()

Class: Customer -Method: Constructor

Class: Transaction-Method: getTransactionType()

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

Tip: To add a method to a class that implements or overrides an existing method in an interface or a superclass, there is a much faster way to add it than by using the Create Java Method wizard. Use the source feature Override/Implement Methods:

Right-click the class in the diagram editor or in the Package Explorer and select Source Æ Override/Implement Methods. In the Override/Implement Methods dialog, all methods that can be implemented or overridden by this class are listed. Select the methods you want to override or implement and click OK to add the method stubs to the selected class.

For example, you can implement the toString method in the Transaction class.

ibm.com/redbooks