PreviousWriting a Class Program Tutorial (UNIX) Collections, Intrinsics and Dictionaries Tutorial (UNIX)Next"

Chapter 15: Inheritance Tutorial (UNIX)

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:

  1. The Account Classes

  2. Some Simple Account Transactions

Time to complete: 25 minutes

15.1 The Account Classes

This topic introduces the Account classes used in this tutorial. In the next session you will use Animator to see how they work together.

We have defined four types of account class. The list below shows the inheritance hierarchy with indentation:

Account

CheckAccount

SavingsAccount

HighRateAccount

CheckAccount and SavingsAccount both inherit from Account, and HighRateAccount inherits from SavingsAccount. This 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.

Look at the source code for the Account and SavingsAccount classes. The Account class implements methods for constructing new accounts "openAccount", and for debiting ("withdraw") and crediting ("deposit") accounts. The SavingsAccount class has far fewer methods, and inherits most of its behavior from Account.

All the classes (except Account) have a method "printStatement". Each type of account prints a different type of statement, but does it when sent the message "printStatement". This is an example of polymorphism.

These classes also contain methods that are not used at all in this tutorial. The reason is that these classes are also used in the GUI bank examples available on Windows NT, Windows 95 and OS/2 platforms.

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.

15.2 Simple Account Transactions

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

  1. Change to the directory with the Object COBOL demo programs (by default $COBDIR/demo/oops).

  2. Run the supplied shell script bankscrp.

    This compiles bank1.cbl and the account classes.

  3. Start animating bank1. Enter the following command line:

    anim bank1

    Animator starts with the statement below tag B001 is highlighted ready for execution.

  4. Step through the first three statements.

    When you step 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.

  5. Step statement 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.

  6. Step the invoke statement below tag A002.

    This takes you to the "setNextAccountNumber" method of Account.

  7. Step the first statement of this method (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.

  8. Step the next statement (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.

  9. Step through the code up until and including the exit method.

    This takes you back to the exit method statement of "setNextAccountNumber".

  10. Step the exit method statement.

    This returns you to the "openAccount" method.

  11. Step the next statement (invoke lsAccount "initAccount" ...).

    The object handle to the newly created account object is in lsAccount, which we are going to initialize with name, balance and account number. Before this happens, execution switches to the Procedure Division of the Account class. This code is only executed once, and is used in this case to execute some code for setting up error handling. The exact point during an application run where class initialization code is executed is not specified. It is however guaranteed to have been executed before the first instance message is executed.

  12. Push the Prfm Step keys.

    This code is the subject of another tutorial, so we execute it without animating through it. Execution switches to the "initAccount" method. The "initAccount" method sets the account attributes with a name, balance and account number.

  13. Step through the code up until and including the exit method.

    This takes you back to the exit method statement of "openAccount".

  14. Step the exit method statement.

    Execution returns to Bank1, which now has an object handle to an instance of the CheckAccount class.

  15. Step the two statements below tag B002.

    This deposits $1000 in the CheckAccount instance. Again, the code for this is all in the Account class.

  16. Step through the Account code until you return to the statement below tag B003 in Bank1.

  17. Step through the two statements below tag B003.

    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.

  18. Step the statements below tag C010.

    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. Example applications used in some of the other Object COBOL tutorials also use these account classes, but register an exception handler to deal with this type of error.

  19. Step through the application until you reach tag B007.

    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 could not 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 have to implement methods which respond to these messages.

  20. Step the statements below B007 to open a HighRateAccount.

    This is the first message sent to the HighRateAccount class, so before executing the "openAccount" method, the class initialization code is run. The execution point appears inside the Procedure Division of the HighRateAccount class. The class initialization code is run the first time the class object receives a message, and in the case of HighRateAccount is used to initialize the exception message file. This is normally initialized by the Account class (as we saw earlier in the tutorial), but it is possible for the HighRateAccount class to raise an exception before the Account class has been initialized, so it needs to be done here as well.

  21. Press the Prfm Step keys to execute the initialization code without animating it. This code is the subject of a different tutorial.

    Execution switches to tag H010 of HighRateAccount. 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.

  22. Step through the rest of the code until you get to tag B008 of Bank1.

    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.

  23. Step through the code to print the statements.

  24. When Animator displays the "Stop run encountered..." message, push the Esc key to exit.

15.3 Summary

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.

PreviousWriting a Class Program Tutorial (UNIX) Collections, Intrinsics and Dictionaries Tutorial (UNIX)Next"