Oocoby 2 K
Oocoby 2 K
Oocoby 2 K
"COBOL 2000"
(Update as of 4/99)
Artur Reimann
• Objects
− Data together with methods for these data form objects
• Data encapsulation
− Objects 'hide' their data
• Classes
− Objects of same kind form classes
• Inheritance
− Sub-classes 'inherit' data and methods of their super-classes
• Polymorphism
− Methods with same name in different classes have different effect
method-3
od-7
Var-1 Var-2
meth Var-3
Var-4
me
tho
Var-5
-4
od
d-6
eth
m
method-5
This picture is a schematic view of an object. An object consists of data (variables, attributes), describing
the state of the real object at any given time, and methods (procedures, programs), needed to access and
to manipulate these data. The data are "encapsulated" in the object, i. e. they are normally not visible and
certainly not changeable from outside the object. This is only possible through methods associated with
the object.
objects (instances)
Objects of the same kind are organized in "classes", schematically shown in the above picture. A class is
something like a "prototype" of its objects. In OOP, you don't describe individual objects, but only their
class.
An individual object, or "instance", will be created by the class, using a method that is associated with the
class just like object methods are associated with the object. Thus, a class can be thought of a special
object with its own methods and its own class data. It is called "factory object", because it is primarily used
to create instances.
Id Division.
Class-Id. class-name-1 Inherits class-name-2 ... .
...
Environment Division.
Configuration Section.
Repository.
Class class-name-3 as "external-class-name-3"
...
Id Division.
Factory.
Environment Division.
...
Data Division.
Working-Storage Section.
...
Procedure Division.
{class methods}
End Factory.
Id Division.
Object.
Environment Division.
Data Division.
...
Procedure Division.
{object methods}
End Object.
End Class class-name-1.
Example 1. Class Definition
Id Division.
Method-Id. method-name-1 ...
...
Data Division.
Working-Storage Section.
...
Linkage Section.
...
Procedure Division [Using ...] [Returning ...] .
...
Invoke object-reference method [Using ...] [Returning ...]
...
Exit method.
End Method method-name-1.
Example 2. Method Definition
Factory Definition
• The factory object contains data and methods associated with the class, for example for creating
objects
• Each class has only one factory object
• Environment Division contains only Input-Output Section
• Factory data and methods are accessible from all object methods of that class
• defined by:
[ID DIVISION.]
FACTORY [IMPLEMENTS interface-name-1].
[Factory Environment Division ]
[Factory Data Division ]
[PROCEDURE DIVISION.
[{factory methods}…]]
END FACTORY.
• IMPLEMENTS clause indicates that the interface of this factory object conforms to interface-1 (see
COBOL INTERFACE definition on page 16)
Object Definition
• The object definition contains data and methods associated with the objects, i.e. the instances of the
class
• Environment Division contains only Input-Output Section.
• Defined by:
[ID DIVISION.]
OBJECT [IMPLEMENTS interface-name-1].
[Object Environment Division ]
[Object Data Division ]
[PROCEDURE DIVISION.
• IMPLEMENTS clause indicates that the interface of this object conforms to interface-1 (see
COBOL INTERFACE definition on page 16)
Method Definition
• Method definitions are contained in the Procedure Division of the Factory or the Object
• The method definition contains object procedures and data associated with the methods.
• Defined by:
[ID DIVISION.]
method-nam e-1 [AS literal-1]
METHOD-ID. GET [ OVERRIDE ].
PROPERTY property-n ame-1
SET
[Method Environment Division ]
[Method Data Division ]
[PROCEDURE DIVISION [USING {data-name-1}...]
[ RETURNING data-name-2].
{statements}...]
END METHOD method-name-1.
• Property (details: see Property on page 18)
− property method
• Override
− overrides inherited method
Inheritance
superclasses
subclasses
'inherit' methods of subclasses
'extension' through new methods
'specialization' through overwriting
Separate classes can have common attributes, which means common data and method definitions. For
example, convertibles and sedans are both cars and have many commonalties. Therefore, it makes sense
to define a class "car" with all the common data and methods. Such a class would be called a
"superclass" of the classes "convertible" and "sedan". The common data and methods are defined only for
the superclass and inherited by the "subclass".
It is possible to define a specialized method that is to be used in place of an inherited method with the
same name, as long as its "signature", i. e. the set of parameters and their properties, "conforms" to the
inherited method (see "Conformance" on page 16).
COBOL 2000 supports the concept of "multiple inheritance", which means that a class can have several
superclasses. Thus the subclass inherits all the methods from all the superclasses.
(object) (method)
Objects communicate with "messages". Data of another object can only be accessed by sending a
message to that object, which causes the execution of a method of the other object.
Such a message has three parts:
1. The receiver of the message. This is an object reference that points to the specific object.
2. The selector. The selector is the name of the receiver's method to be activated.
3. Arguments, to be passed to the selected method, analogous to the arguments in a CALL statement.
Messages in COBOL
• Explicit message through Invoke statement
BY REFERENCE
INVOKE object-reference "method" [USING
BY CONTENT {argument ...}]
BY VALUE
[ RETURNING return-item ]
− object reference:
∗ class name
∗ 'object reference'
− method:
∗ Literal
∗ Data element (for 'universal ' O-R only)
• Implicit message through inline method invocation:
Conformance
• Conformance
− Interface a conforms to interface b, when
∗ all methods of b are also accessible from a
∗ the number and characteristics of corresponding parameters match
− object with interface a "is an" object with interface b
• Inheritance
− Interface of sub-class conforms to interface the super-class (e. g. "convertible is a motor
vehicle")
− COBOL: CLASS-ID. ... INHERITS ...
Polymorphism
object-1 me me object-2
e-1-1 tho -e1-1 tho
hod hod d-2 (of class-b)
(of class-a) met
M
d-2 Met
m
od-7
od-7
method-3
method-3
Var-1 Var-1 Var-2
meth
Var-2
meth
Var-3 Var-3
Var-4 Var-4
me
me
tho
tho
Var-5 Var-5
4
4
d-6
d-
d-6
d-
ho
ho
et
et
m
method-5
m
method-5
One of the most important characteristics of messages in OOP is that the message itself does not
automatically imply its specific effect. This is because a message can be sent to different objects, as long
as each of these objects have a method of the specified name that takes the same number and
characteristics of arguments, i. e. conforms to the message. There are three cases:
• Hierarchic polymorphism
− Object reference identifies object in specific class hierarchy
− COBOL: USAGE OBJECT REFERENCE class-name (without 'ONLY')
• Polymorphism using interface definition
− Object reference identifies an object of an arbitrary class that implements a specific interface
− COBOL: USAGE OBJECT REFERENCE interface-name
• Universal Polymorphism
− Object reference identifies an object of an arbitrary class
− COBOL: USAGE OBJECT REFERENCE (without class-name)
− If the selected object does not conform, an "exception condition" exists (see below)
Property
• Format:
property-1 OF object-identifier-1
− abbreviated way of addressing the value of an object variable
• Prerequisite:
− The object variable property-1 is defined:
GET
data-name PROPERTY [WITH NO ]
SET
− for use as a sending item:
∗ without WITH NO GET: implicit generation of method Get Property data-name
− for use as receiving item:
∗ without WITH NO SET: implicit generation of method Set Property data-name
• Example:
Move residence of employee-master to residence of salary-master
is equivalent to
Invoke employee-master Get Property residence Returning temp-1
Move temp-1 to temp-2
Invoke salary-master Set Property residence Using temp-2
Generic Classes
• Generic class (parameterized class)
• defined by:
CLASS-ID. class-name-1 ...
USING { parm-name-1 }... .
• class-name-1 references classes that are fully specified only by the client
• parm-name-1 is a place holder for a class-name to supplied later
• specification in repository of client:
class-name-1 ... EXPANDS class-name-2 USING {class-name-3}...
EC-OO
EC-OO-CONFORMANCE
EC-OO-EXCEPTION
EC-OO-INTRINSIC
EC-OO-METHOD
EC-OO-NULL
EC-OO-RESOURCE
EC-OO-UNIVERSAL
EC-OO-IMP
• Example
...
Declaratives.
Use after exception EC-OO-Exception.
...
Display function exception-location function exception-statement
...
End Declaratives.
...
>>Turn EC-OO checking on
...
>>Turn EC-OO checking off
...
object: H
object reference: H
"New"
"sayHello"
object: H
object reference: H
"New"
"setMsg"
"sayHello"
Identification Division.
Method-Id. sayHello.
Procedure Division.
Display msg.
End Method sayHello.
Identification Division.
Method-Id. setMsg.
Data Division.
Linkage Section.
01 in-msg pic x(30).
Procedure Division using in-msg.
Move in-msg to msg.
End Method setMsg.
End Object.
End Class Hello.
Identification Division.
Program-Id. Client.
Environment Division.
Configuration Section.
Repository.
Class Hello.
Data Division.
Working-Storage Section.
01 H usage object reference hello.
Procedure Division.
Invoke Hello "new" returning H
Invoke H "setMsg" using by content "Hello World!"
Invoke H "sayHello"
Exit Program.
End Program Client.
Class: Hello
Program: Client
Superclass
"SayHello"
object reference: H
"New"
"setLang"
Subclass "SayHello"
Identification Division.
Object.
Data Division.
Working-Storage Section.
01 lang-table.
02 pic x(31) value "EHello World from Sample 3!".
02 pic x(31) value "DHallo Welt von Beispiel 3!".
02 pic x(31) value "FAttention monde d'example 3!".
01 lang-table-x redefines lang-table.
02 lang occurs 3 indexed by x1.
03 lang-code pic x.
03 lang-msg pic x(30).
Procedure Division.
Identification Division.
Method-Id setLang.
Data Division.
Linkage Section.
01 in-lang pic x.
Procedure Division using in-lang.
Set x1 to 1
Search lang
At End
Invoke self " setMsg"
using by content "Wrong language!"
When lang-code(x1) = in- lang
Invoke self " setMsg"
using by content lang-msg(x1)
End-Search
Exit Method.
End Method setLang.
End Object.
End Class Hello1.
Identification Division.
Program-id. Client.
Environment Division.
Configuration Section.
Repository.
Class Hello1.
Data Division.
Working-Storage Section.
01 H usage object reference Hello1.
Procedure Division.
Invoke Hello1 "New" returning H
Invoke H "setLang" using "F"
Invoke H "sayHello"
Exit Program.
End Program Client.
"New"
"setMsg"
a c
a b
"HelloCreate"
c
"sayHello"
b
Identification Division.
Factory.
Procedure Division.
Identification Division.
Method-Id. HelloCreate.
Data Division.
Linkage Section.
01 in-msg pic x(30).
01 anObject usage object reference Self.
Procedure Division using in- msg returning anObject.
Invoke self "New" returning anObject
Invoke anObject "setMsg" using in-msg.
End Method HelloCreate.
End Factory.
Identification Division.
Object.
Data Division.
Working-Storage Section.
01 msg pic x(30).
Procedure Division.
Identification Division.
Method-Id. sayHello.
Procedure Division.
Display msg.
End Method sayHello.
Identification Division.
Method-Id. setMsg.
Data Division.
Linkage Section.
01 in-msg pic x(30).
Procedure Division using in-msg.
Move in-msg to msg.
End Method setMsg.
End Object.