Writing a Class Program Tutorial (Windows & OS/2) | Collections, Intrinsics and Dictionaries Tutorial |
This tutorial is intended to demonstrate how inheritance works in Object COBOL. It uses the example of different types of bank account, and shows how they can inherit common behavior from a single class, while adding new behavior or changing as necessary for individual types of account.
This tutorial consists of the following sessions:
Time to complete: 25 minutes
This topic introduces the Account classes used in this tutorial. We will use the Class Browser to explore the code in these classes, before animating it in the second session of this tutorial.
>>To start the Browser with the account classes
This starts the on-line tutorial Inheritance.
This starts the Class Browser and loads the account classes.
You can see that there are four types of account class:
Account
CheckAccount
SavingsAccount
HighRateAccount
The hierarchy view shows that the Account class is the superclass of all other account types. Account is an abstract class which implements the behavior common to all the different types of bank account.
Account has one class method, "openAccount", and several instance methods. These include methods for all the things you would expect to be able to do with a bank account, like "deposit" and "withdraw".
The SavingsAccount class implements far fewer methods than Account. It has no method for opening accounts, depositing or withdrawing money. It inherits all the code for these methods from Account. It does have a method to add interest, because this is not behavior common to all types of account.
It also has an implementation of "printStatement". All the account classes have their own implementation of "printStatement", because every different type of account prints a different type of statement.
This is an example of polymorphism.
In the next session, you will use the Bank program to see how inheritance works in these classes. This is a simple program which creates accounts and sends messages to them.
In this session you will animate bank1.cbl, to see some simple account transactions and how inheritance works in practice. The instructions below assume that you are continuing directly from the previous section and still have the Class Browser up and running with the account classes.
>>To animate bank1
This starts Animator V2, and loads bank1.int for execution. You are now in Bank1 (bank1.int). The first group of statements (below tag B001) creates a new CheckAccount for a customer.
When you step the invoke CheckAccount
,
execution switches to the line below tag A001 in the Account class.
CheckAccount does not implement the "openAccount" method, but inherits
it from Account.
invoke super "new"
.
This sends the "new" message to this object, but tells the OO RTS to start looking for methods in the code for the superclass of Account (class Base). It returns an instance of CheckAccount.
invoke
statement below tag A002.
This takes you to the "setNextAccountNumber" method of Account.
if nextAccountNumber
= 0
).
Data item nextAccountNumber
is declared
with a value clause of 0. When the account class or any of its subclasses
is initially loaded this data item is set to 0. The method "setNextAccountNumber"
tests this value, and if it is zero, sets an initial value. This is
the first time the CheckAccount class has received the "openAccount"
message, so nextAccountNumber
needs initializing.
invoke self "getFirstAccountNumber"
).
Execution switches to the "getFirstAccountNumber" method of
CheckAccount. In this case, self
refers to the
CheckAccount class object; although we were executing code implemented
by the Account class, the original "openAccount" message was sent
to the CheckAccount class object, which inherited its "openAccount"
method from Account.
This method returns a value for the "openAccount" method to start numbering accounts. Each of the subclasses of Account starts account numbers from a different value, and they all implement "getFirstAccountNumber" to return the starting value.
exit
method
.
This takes you back to the exit method
statement of "setNextAccountNumber".
exit method
statement.
This returns you to the "openAccount" method.
invoke lsAccount "initAccount"
...
).
The object handle to the newly created account object is in
lsAccount
. The "initAccount" method sets the account
attributes with a name, balance and account number.
exit
method
.
This takes you back to the exit method
statement of "openAccount".
exit method
statement.
Execution returns to Bank1, which now has an object handle to an instance of the CheckAccount class.
This deposits $1000 in the CheckAccount instance. Again, the code for this is all in the Account class.
These withdraw $50 from the CheckAccount instance. This time
the invoke
switches execution to code inside CheckAccount.
This is because the CheckAccount class implements its own "withdraw"
method, which checks the withdrawal against the value allowed for
the overdraft.
If the withdrawal were to exceed the overdraft set for this instance of CheckAccount, the object would raise an exception (error condition). In this case, the withdrawal amount is OK, so execution proceeds as normal.
The application in this case has not registered an exception handler to deal with account exceptions, so the error would get trapped at the system level which would display the error number and halt application execution.The BankApplication example used in some of the other Object COBOL tutorials also uses these account classes, but it registers an exception handler so that it can deal with this type of error.
Each time you open a type of account which Bank1 hasn't used before, you have to step through the code to get an initial account number.
As you execute the code, you will see that most of the code
for the different types of bank account is inherited from the Account
class, with only some methods being implemented in the subclasses.
The subclasses all inherit from the Account class with data (see the
class-id
statements at the top of each program).
This means that as well as inheriting the methods of Account, they
also have direct access to the data items declared in account. All
the different account types can access variables balance
,
aName
and accountNumber
.
Without the with data
clause in the class-id
of the Account subclasses, they would not be able to access this data
directly. Instead, they would have to send messages to the superclass
(invoke super...
) to access this data. The programmer
of the Account class would need to implement the methods which would
respond to these messages.
HighRateAccount implements its own version of "openAccount",
to check the amount of cash being used to open the account. But once
it has verified this, it still uses the "openAccount" method of Account,
by sending a message to super
.
This code demonstrates the use of polymorphism between objects descended from a common class. All account types implement the "printStatement" method in order to print out a statement suitable for the different account types.
This concludes the tutorial on inheritance. In this tutorial you learned:
Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names
used herein are protected by international law.
Writing a Class Program Tutorial (Windows & OS/2) | Collections, Intrinsics and Dictionaries Tutorial |