Class Browser Tutorial (Windows & OS/2) | Writing a Class Program Tutorial (Windows & OS/2) |
This tutorial is a gentle introduction to Object COBOL programming, using a class with some simple behavior. We will call this the Parrot class. In the tutorial you will create instances of the Parrot class. An instance of the Parrot class is a parrot object, and having created parrots, you can send messages to them.
This tutorial consists of the following sessions:
Time to complete: 20 minutes.
This section introduces you to the Parrot class, describing its behavior and public object interface. The object interface is the list of methods implemented by an object, together with descriptions of the parameters passed to and from the object by each method.
The public object interface is a censored list of this information; it does not describe the object's private methods. A private method is one used by the object itself; it should not be invoked from outside the object.
The interface for the Parrot is split in two:
A class is an object in its own right; it can have behavior and data of its own, which is different to the behavior and data of instances of the class. The main function of most classes is to implement the behavior needed to create and initialize new instances.
Instances of the Parrot class are windows displayed on your desktop. Parrot windows are numbered sequentially, with the number shown in the title bar. You can't resize or close Parrot windows from the desktop; they are controlled purely by the application which creates them.
A parrot instance traps any message sent that it doesn't understand, and echoes it back. The default behavior when an object receives a message it doesn't understand, is to display an error message and then terminate the application.
The Parrot class public object interface is listed below.
Class method | Description |
---|---|
"new" returning anObject
|
Returns an instance of the Parrot class, with the reference
in anObject
|
"howMany" returning aNumber
|
Returns the number of parrot instances created so far. |
"sayHello" using aName
|
Display "Hello aName" in the Response field of the Parrot window. You must terminate the string in aName with a space. |
"whatNumber" returning aNumber
|
Return the number of this parrot instance (all parrots are
numbered sequentially as they are created). |
In addition to these methods, the Parrot class inherits all those implemented by its superclass (Window). The superclass is the parent from which this class inherits its basic behavior.
The Parrot class inherits all the class methods of Window, and its instances inherit all the instance methods of Window. This means that Parrot responds to all the messages given in the Window public interface as well as its own. Inheritance is explained in more detail in the tutorial Inheritance.
The Window class is part of the class library supplied with Object COBOL. If you want to look at the Window public interface, it is in your on-line Object COBOL Class Library Reference.
You can now move on to the next session, which uses Animator V2 to show you message sending.
In this section you will create instances of the Parrot class, and send messages to the instances, using the supplied program, hello.cbl.
Program hello.cbl is not an Object COBOL class; it is a piece of procedural code which uses Object COBOL objects. To communicate with the objects hello.cbl uses the following Object COBOL syntax:
class-control
paragraph
Declares the classes used by the programs, and links them to their filenames.
object reference
data type
Declares a variable to hold object references.
invoke
verb
Sends a message to an object.
You will now animate hello to see some simple object behavior. The code demonstrates the following points:
>>To animate hello
This starts the on-line tutorial Objects and Messages.
This starts Animator V2 and loads hello.cbl.
Animator V2 syntax-checks the program and prepares it for execution.
The line below tag H001 is highlighted ready for execution.
invoke Parrot "new" ...
).
This sends the "new" message to the Parrot class, which creates
an instance of itself, returning an object handle to it in aParrot
.
The new instance opens the Parrot 01 window on your desktop.
aParrot
with the Examine function (short-cut - double-click on aParrot
).
This data item is declared with usage object reference
,
which is a new COBOL datatype introduced for Object COBOL programming.
It now holds a handle to the object you have created.
32-bit environments:
When you examine it on 32-bit Object COBOL systems, an Object
Inspector window appears. This tells you that the object is an instance
of the class parrot.
16-bit environments:
When you examine it on 16-bit Object COBOL systems, the Animator
V2 monitor window just displays the value of the object handle.
When programming in Object COBOL, you use the object handle as an address to send messages to the object. You can pass object handles between different programs or objects as parameters, in effect enabling you to pass an object between different parts of an application.
invoke aParrot "sayHello"
).
Parrot 01 displays "Hello Polly" in the Response field, and "sayHello" in the message field. Parrots always repeat the messsage sent.
invoke aParrot "saySomethingElse"
).
Parrots don't understand this message; "saySomethingElse" is not a method implemented by the Parrot class, or any of its superclasses. The parrot traps this error and redisplays the message it didn't understand.
The default behavior when you send an object a message it doesn't understand is that it displays an error message and terminates the application. The Parrot class program overrides this behavior by reimplementing the "doesNotUnderstand" method which objects otherwise inherit from Base (part of the Class Library).
invoke aParrot "whatNumber"
).
This returns the ID of the parrot in parrotNumber. If you examine this data item, you can see it has a value of 1.
This creates a new instance of the Parrot class, and puts the
object handle into aParrot
. The first parrot you
created, Parrot 01, still exists. But you have now overwritten the
only data item you had giving you its object handle, so you can no
longer send messages to it.
This is something you must be careful about when programming with Object COBOL; once you lose the object handle to an object, you have no easy way of sending it messages and no way of destroying it. You can get a collection of all the handles for the instances of a particular class by sending the message "allInstances" to the class object. However, in most real applications you would not know who had created each particular instance, and there would be no safe way of knowing which ones you could destroy.
These send messages to the new instance of Parrot.
This queries the class data of Parrot. If you examine parrotNumber
you can see that it contains the value 2, the number of parrot instances
so far created.
Sending the "finalize" message to an object deallocates its storage. You should finalize any object your application has finished with. Once you have finalized an object, you should no longer try to send messages to its object handle, as the results are unpredictable.
The proposed ANSI standard for Object-Oriented COBOL includes automatic garbage-collection. With garbage-collection, it is the run-time system's responsibility to dispose of objects that are no longer in use. When the ANSI standard is fixed and Micro Focus implement it, you will no longer need to include code to finalize objects.
This completes the tutorial on creating objects and sending messages. You have seen:
Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names
used herein are protected by international law.
Class Browser Tutorial (Windows & OS/2) | Writing a Class Program Tutorial (Windows & OS/2) |