Classes

A class defines the data members and function members for object instances that are created from that class.

In managed COBOL, each class inherits from exactly one other class. If the inherited class is not specified (in the INHERITS clause), then the class inherits from System.Object (.NET) or from java.lang.Object (JVM).

class-specification

class-header constraints-paragraph attribute-clause Static or Instance Fields static-or-instance-member type-specification

class-header

access-modifier type-specifier type-specifier attribute-clause generic-using-phrase

Example

class-id A.
01 sField string.
method-id doStuff.
   ...
end method.

class-id nestedClass.
...
end class.

end class.

See also the Core sample, which is available from Start > All Programs > Micro Focus Visual COBOL > Samples, under COBOL for .NET .

Class Header Keywords

ABSTRACT
Declares a class that is only intended to be used as a base class. An abstract class can be thought of as an incomplete class.
FINAL
Declares a class whose definition is complete. The FINAL modifier on a class implies that the class cannot be inherited.
STATIC
Declares a class that cannot be instantiated.
PARTIAL
Indicates that the declaration is one of two or more parts that make up the full declaration. Partial classes enable you to split a class declaration into logical parts.
INHERITS
Defines that a class is inherited, where type-specifier specifies the class that is inherited. Inheritance enables a class to implicitly contain members of its inherited class type.
IMPLEMENTS
Indicates that an interface is implemented, where type-specifier specifies the interface that is implemented.
SERIALIZABLE
This allows the class to be serialized in .NET COBOL via the System.Runtime.Serialization.Formatters.Binary class, and in JVM COBOL via the java.io.ObjectOutputStream class.

Abstract Classes

An abstract class cannot itself be instantiated, but non-abstract classes can inherit from the abstract class, and can be instantiated. Because an ABSTRACT class has to be inherited, it cannot be FINAL.

An abstract class typically contains abstract methods, which are not implemented in the class declaration, like method declarations in an interface. It is however possible for an abstract class to define non-abstract methods. A class inheriting the abstract class, and which is not itself abstract, must provide implementations for all the abstract methods defined in the abstract class. Only an abstract class can contain abstract methods.

Use OVERRIDE when implementing the members of an abstract class in the non-abstract members of the derived class, as shown in the following example:

       class-id. Animal abstract.
       method-id MakeNoise abstract. 
      *> No implementation allowed
       end method.

       end class.

       class-id Dog inherits type Animal.
      *> Overrides the MakeNoise method in the Animal class
       method-id MakeNoise override.
           display "Woof!"
       end method.
       end class.

In this example, the abstract class Animal contains the abstract method MakeNoise. Because the non-abstract class Dog inherits from the abstract class Animal, it must provide an implementation of MakeNoise using the OVERRIDE modifier.

An abstract class can contain non-abstract methods in addition to abstract methods.

A class that inherits an abstract class must also be declared as an abstract class if it does not provide implementation of the inherited class' members.

Final Classes

A final class cannot be inherited. Because of this, methods in a final class cannot be overridden.

Declaring a class as FINAL is useful to prevent subversion of the class. It prevents a subclass from potentially changing the intended class functionality by overriding its intended behavior.

Nested Classes

Nested classes can access static fields, properties and methods within the containing classes.

The optional SHARING PARENT phrase in the nested class definition enables instance methods and properties in the nested class to access the instance fields, properties and methods in the containing class.

    class-id ContainingClass. 
    ...
    class-id Nested1. 
        ... 
    end class.
    class-id Nested2 sharing parent. 
       ... 
    end class. 
    end class.

Nested Classes with SHARING PARENT

Nested classes with the SHARING PARENT phrase may only be instantiated from within an instance method in the enclosing class. They can access the instance members from that containing class, even when those instance members are private.

Such a nested class contains an implicit reference to an instance of its containing class. The nested class uses this reference to access the enclosing class's instance members. This means that the containing class instance will not be garbage collected until all references to the inner class held externally have themselves been garbage collected.

For example:

    class-id ContainingClass. 
    01 companyName string value "Micro Focus".
    
    method-id instance1.
    01 o type Nested2.
        set o to new Nested2
        ...
    end method

    class-id Nested2 SHARING PARENT. 
    method-id method2
        display companyName
    end method.
    end class.

    end class.

Nested Classes without SHARING PARENT

Nested classes defined without the SHARING PARENT phrase may be declared as public, private or internal. Depending on the visibility specified, they can be instantiated from any context, not just from the containing class, and not just from instance methods. They can only access instance members of the containing class using an explicit object reference.