Example: defining a subclass (with methods)
The following example shows the instance method definitions for the CheckingAccount subclass of the Account class.
The processCheck method invokes the Java™ instance methods getAmount and getPayee
of the Check class to get the check data. It invokes the credit
and debit
instance
methods inherited from the Account class to credit the payee and debit
the payer of the check.
The print
method overrides
the print
instance method defined in the Account
class. It invokes the overridden print
method to
display account status, and also displays the check fee. CheckFee
is an instance data item defined in the subclass.
(The Account class was shown in Example: defining a method.)
CheckingAccount class (subclass of Account)
cbl dll,thread,pgmname(longmixed)
Identification Division.
Class-id. CheckingAccount inherits Account.
Environment Division.
Configuration section.
Repository.
Class CheckingAccount is "CheckingAccount"
Class Check is "Check"
Class Account is "Account".
*
* (FACTORY paragraph not shown)
*
Identification division.
Object.
Data division.
Working-storage section.
01 CheckFee pic S9(9) value 1.
Procedure Division.
*
* processCheck method to get the check amount and payee,
* add the check fee, and invoke inherited methods debit
* to debit the payer and credit to credit the payee:
Identification Division.
Method-id. "processCheck".
Data division.
Local-storage section.
01 amount pic S9(9) binary.
01 payee usage object reference Account.
Linkage section.
01 aCheck usage object reference Check.
*
Procedure Division using by value aCheck.
Invoke aCheck "getAmount" returning amount
Invoke aCheck "getPayee" returning payee
Invoke payee "credit" using by value amount
Add checkFee to amount
Invoke self "debit" using by value amount.
End method "processCheck".
*
* print method override to display account status:
Identification Division.
Method-id. "print".
Data division.
Local-storage section.
01 printableFee pic $$,$$$,$$9.
Procedure Division.
Invoke super "print"
Move CheckFee to printableFee
Display " Check fee: " printableFee.
End method "print".
*
End Object.
*
End class CheckingAccount.