Introduction to Object COBOL | Object COBOL Tutorials (Windows & OS/2) |
This chapter describes the key concepts supported by an Object-Oriented Programming System (OOPS), and how they are implemented by Micro Focus Object COBOL. Micro Focus Object COBOL is based on the proposed ANSI standard for OO COBOL.
OO programming introduces the following new ideas and concepts:
You need to understand how these have been implemented in Object COBOL to use it as an effective OO language.
The following sections describe each OO concept in turn, and how it is implemented in Object COBOL. Reading this chapter is not enough to teach you good OO programming; for more information about learning OO consult the section Learning More About Object-Orientation in the chapter Introduction to Object COBOL.
An object is an encapsulation of data and the procedures to operate on that data. The data is known as the object's attributes, and the procedures are known as its methods. Every object in an object-oriented application has a unique object identifier, allocated to it at creation and fixed for its lifetime.
Many of the objects in an OO application represent objects in the real world. For example, a banking system would include objects to represent customers, accounts and ledgers. The attributes of an account would include the balance, and its methods would include Debit, Credit, GetBalance. Figure 2-1 shows two ways of representing such an object.
The user of an object can only find out about or change its attributes by making requests to the object. These requests are known as messages, and each message invokes a method supported by the object. The object interface is a description of all the messages to which the object responds.
To find out the balance of an account, you would send an account object the message GetBalance. The actual representation of the data is known only to the account object. As long as the object interface remains the same, a programmer can change the internals of how the object represents and operates on data, without affecting the rest of the system. It is up to the GetBalance method to ensure that it always provides data in the expected format.
A class is a template for creating objects; it embodies all the information you need to create objects of a particular type. An account class creates account objects and a ledger class creates ledger objects. An account object is said to be an instance of the account class.
In Object COBOL, a class is an Object COBOL program, which consists of a set of nested programs. At run-time, when the class is loaded, the run-time system creates a class object, which represents the class. The class object enables you to create instances of the class.
In the Object COBOL class, the outer program contains all the attributes and methods specific to the class object. The object program contains the attributes and methods specific to each instance object. Figure 2-2 shows a class diagramatically.
Why does the class object have separate methods and data to the objects it creates? A class object is an object itself; it provides a set of services and may hold attributes. Although a class is an object, it does not have the same behavior as the instances it creates.
A class is like a printer's plate, printing identical forms. A plate enables you to print a form, but is not a form itself. The Object COBOL class program contains the code and data for both the class and the object. The Object COBOL run-time enables you to create many instances of the class (see Figure 2-3).
Figure 2-3: Creating instances of a class
Each instance is like an independent program in your run-unit, although only the object data exists separately in memory for each object. The code is shared between the instances (see Figure 2-4) .
Figure 2-4: Code is shared by all instances
Methods are the code which implement the behavior of an object. In Object COBOL, each method is a separate program nested within the class or object program. An object method can access its own data, the instance data and data declared in the class program data division (except for class data). A class method can access its own data and the class data.
Note: The implementation of class data and instance data is different between Micro Focus Object COBOL and the proposed ANSI standard for OO COBOL. ANSI uses the working-storage section for object data, whereas Micro Focus instances can access class working-storage and use it for instance initialization data.
Inheritance enables you to reuse code by identifying common characteristics between classes. For example, in our banking example there are likely to be several types of account; for instance, savings accounts, high rate accounts and checking accounts. There are some features that all these accounts have in common; a balance attribute, methods Credit and GetBalance. But there are also differences; a savings account pays interest, while a checking account may allow overdrafts.
An Account class implements all the methods and attributes common to all its subclasses. The subclasses implement the attributes and methods which they uniquely require. In this example, the Account class is an abstract class; it only provides behavior for subclasses and you never create actual instances of the Account class.
A deposit account implements its own version of Debit which does not allow overdrafts. Figure 2-5 shows a possible inheritance hierarchy for Bank accounts.
Figure 2-5: Bank account inheritance
Inheritance also provides easy updates to the system later on, by adding new subclasses as required. For example, the bank might introduce two new types of savings account; one with instant access and a high interest account where you had to give notice of a withdrawal. These could be subclassed from the Savings Account class, with additions to provide the new behavior required. Because they still respond to the message interface used by all account objects, the changes do not ripple through the rest of the system.
Object COBOL is supplied with a Class Library, in which all classes are ultimately descended from class Base. Base provides methods which are required by all classes, which include methods for creating and destroying objects. The classes you write are also likely to be subclasses of Base. You can create your own root class and subclass from that, but you may need to duplicate some of the services provided by Base.
In Object COBOL, a subclass has access to all the class methods of its
superclasses. An instance of a subclass has access to all the object
methods of its superclasses. Data can also be inherited, depending on the
clauses in the class-id
header.
Polymorphism is an important part of any object-oriented programming system. Polymorphism means that the same message sent to different objects can invoke different methods.
For example, consider a graphics drawing system which has objects representing squares and objects representing circles. The methods for drawing squares and circles are different, but the display controller can send the message Draw to any graphical object, without caring what type it is. The receiver of the message will execute its own Draw method, producing the correct result (see Figure 2-6).
As already explained, a message is the way you request an object to perform a service. A message always consists of the following:
Messages may also optionally contain input and output parameters. Where there is an output parameter, the sender will be expecting a reply to its message.
The object reference enables the run-time system to find the object for which the message is intended. The target of a message is known as the receiver. The Object COBOL run-time system uses dynamic binding to determine the receiver of a message. This means that the receiver of a message is determined at run-time rather than at compile-time.
The method selector is the text of the message; it tells the
receiver which method it should invoke. Object COBOL uses a new verb,
invoke
to send messages. Figure 2-7 shows the
components of a message both diagramatically and in Object COBOL syntax.
Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names
used herein are protected by international law.
Introduction to Object COBOL | Object COBOL Tutorials (Windows & OS/2) |