Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Fundamentals of object-oriented programming

Object-oriented software development is based on some key techniques that make it possible to design software systems that are easy to understood, modify and reuse.
The most important among them are:

  • defining objects that exchange messages

  • building classes primarily intended for general use

  • use of inheritance as a structuring mechanism and a means of avoiding multiple implementations

  • use of so-called virtual methods such as polymorphism and “late binding” for concrete specialization.

A classically programmed application system essentially consists of an algorithm and a data structure. In an object-oriented application system, by contrast, the data can only be indirectly modified by functions.
Some of the basic concepts of object-oriented programming are explained below.

Objects

An object is defined by a class and consists of a combination of data and functions. These functions are called methods of the object. Objects communicate with one another by exchanging messages, which in turn are sent via calls to methods.
Whenever an object is created, it is always assigned an object reference value that uniquely identifies it for the lifetime of the object.

Classes

Classes are defined by class definitions and designate a set of objects with their attributes and methods.

Object references

An object reference is an implicitly or explicitly defined data item. The contents of the object reference uniquely references an object and its associated information.
Implicitly defined object references are the predefined object references and object references returned from an object view. Explicitly defined object references are data items defined by a data description entry specifying a USAGE OBJECT REFERENCE clause.

Predefined object references

A predefined object reference is an implicitly generated data item referenced by one of the identifiers NULL, SELF or SUPER.

Object instances

Whenever a new object is to be generated, the system creates a new copy of the attributes of the class and thus creates an object instance. The methods of the class then apply to this new object as well.

Factory objects

In OO-COBOL, every class contains one and only one special object, the so-called factory object, which is described by the FACTORY definition of the class. The factory object is responsible for creating object instances (objects) of a class.

Methods

Methods are operations, functions and statements that can be executed by an object.

Inheritance

The basic idea behind inheritance is to use a class hierarchy, which is structured from the “general” to the “special”, so that classes which are “lower” in the hierarchy can inherit the methods and attributes from the classes above them. The concept of inheritance helps prevent the redundant duplication of code for similar applications.
A class may inherit from more than one other class; this is called multiple inheritance.

Polymorphism

Polymorphism means that the same message sent to different objects can trigger different actions depending on the type of object, i.e. can call different methods, provided their interface is identical.

Interfaces

Every object has an interface consisting of the name and parameter specifications for every method of an object, including the inherited methods.
Every class has two interfaces: one for the factory object and the other for the remaining objects.

Interface

OO-COBOL provides the language element Interface for defining an interface - independently of a concrete object or class. Here it is not necessary to record the complete object interface in an Interface. Rather, with the aid of an Interface which only defines the common part of the interfaces of unrelated classes, it is possible to process objects of these different classes in a uniform manner.

Conformance

Conformance is a unidirectional relation from one interface to another interface and from an object to an interface.
Conformance is one of the underlying principles that enables fundamental features such as inheritance and interface definitions, for example.
If there is conformance between two classes, all interfaces of one class can also be used in the other class.
Conformance is bound to specific rules, which are generally checked at compile time. Note, however, that if an object view is used or a method for a universal object reference is called, this check occurs only at runtime.

Schematic examples

Example 12-19

for polymorphism, late binding

Class A                          {M(A)} Set of available methods
  Method M
    Display ’AAA’

Class B inherits A               {M(B)} Set of available methods
  Method M override
    Display ’BBB’

User program
01 aref usage object reference A     a)

  invoke B ’NEW’ returning aref      b)
  invoke aref ’M’                    c)

             Output --> BBB

  invoke A ’NEW’ returning aref      d)
  invoke aref ’M’

             Output --> AAA

Notes

a)

An object reference that can point to objects of class A and all classes that inherit from it.

b)

aref points to a class B object.

c)

The method actually called need not be one of those available in the class specified on defining the object reference (which would be class A and thus the method M(A)), but will depend on the content of the object reference at the time of invoking the method: the method is one that is available in the class of the current object and could be either defined in the class itself or inherited from an object class.
In this case, this is the method M(B) .

d)

Here, aref now points to a class A object, and an “externally” identical “invoke” as in c) produces a different output, since the current object is a different object.



Useful tips

  • This example illustrates the concept of a “late binding”, since the assignment of the current method to the method call can only occur at runtime, i.e., when the current contents of the object reference (and thus the class in which the method is to be found) is determined.

  • This effect occurs only when a method of a super class is overwritten in an inheriting class. Otherwise, the called method is the one from the super class.

  • The important point to remember is that besides the identical method names, even the interface of the corresponding methods must be the same (so that a subclass object can be addressed like an object of its super class even on the part of the interface). This is what effectively produces the conformance and a form of polymorphism.

Example 12-20

for SELF

Class A                  {M(A),N(A)} Set of available methods
  Method M
    Display ’AAA’
  Method N
    invoke SELF ’M’                     c)

Class B inherits A       {M(B),N(A)} Set of available methods
  Method M override
    Display ’BBB’

User program
01 aref usage object reference A 

  invoke B ’NEW’ returning aref         a)
  invoke aref ’N’                       b)

         Output --> BBB

Notes

a)

The current object is a class B object.

b)

Method N (A) is called here, since no local N was defined in the inheriting class B (which is the source of the current object).

c)

Executing N leads to a further method call. In this case, SELF stands for the object with which the method was called, i.e. an object of class B.
This again results in the situation depicted in the prior example:
The method that applies to an object of class B is the one available in class B, i.e., from the set {M(B), N(A) }, and does not have to be a method that is available in the class containing the invoke with SELF (which would be from the set {M(A), N(A) }).


Useful tips

  • This also represents a form of “late binding”.

  • If ’no-one’ inherits from class A, then the method called with SELF is exactly the same as the one also available in class A.

  • By using SELF, methods can invoke other methods for their “current object“, which may not exist at compile time. This feature can be very useful, since it is not always possible to predict at compile time “who” will inherit the corresponding class in the future and possibly replace those methods by methods of its own.

Example 12-21

for SUPER

Class A                  {M(A)} Set of available methods
  Method M
    Display ’AAA’

Class B inherits A       {M(B),N(B} Set of available methods
  Method M override
    Display ’BBB’
  Method N
    invoke SUPER ’M’                c)

Class C inherits B       {M(C),N(B} Set of available methods
  Method M override
    Display ’CCC’

User program
01 aref usage object reference A

  invoke C ’NEW’ returning aref     a)
  invoke aref as C ’N’              b)

        Output --> AAA

Notes

a)

The current object is a C object.

b)

The method must be found in the set of methods available for C objects, i.e., in {M(C) ,N(B) }. Consequently, the methods defined in class B are invoked.

c)

This leads to an internal method call for the same object for which method N(B)  was invoked (i.e., still the class C object). SUPER indicates that the method is now to be found among the methods available in the super-class (for multiple inherited classes, the appropriate class should be defined with a class name qualification!). This super-class is not relative to the current object, but statically refers to the class containing the method call with SUPER, i.e. class A. 
Method M in 3) is thus to be found among the methods available in class A, i.e. the set {M(A) }.


Useful tips

  • SUPER enables you to control the method selection by restricting the search to the statically known inheritance hierarchy instead of the methods available for the current object.

  • By overwriting a method and using SUPER, it is possible to execute additional operations with a “new” method, while retaining the same interface. Since the methods available in each class must have unique names, and the “new” method makes thereplaced method invisible, a facility to use this overwritten method of the super-class is required. The language element SUPER is used for this purpose.