Previous Topic Next topic Print topic


Classes

A class defines the data members and function members for object instances that are created from that class. Managed COBOL allows a class to inherit from one or more other classes.

class-specification

class-header constraints-paragraph constraints-paragraph static-or-instance-member type-specification

class-header

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

Example

class A.
01 sField string.
Method doStuff.
   ...
end method.

class nestedClass.
...
end class.

end class.

See also the Core sample, which is available from Start > All Programs > Micro Focus Studio Enterprise Edition x.x > 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 that must be inherited to access its members.
FINAL
Declares a class whose definition is complete. The FINAL modifier on a class implies that the class cannot have subclasses.
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 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.

Abstract Classes

An abstract class is a class that cannot be instantiated, but can only be inherited from. Because of this, an abstract class is typically used as a base class. Members of an abstract class are only declared in the abstract class, and cannot be implemented. Because abstract classes must be inherited from, the FINAL modifier cannot be used when declaring an abstract class.

An abstract class typically contains abstract methods, which cannot be implemented in the class declaration, similar to method declarations in an interface. Because abstract classes cannot be instantiated, subclasses that inherit the abstract class must provide the implementations of abstract methods defined in the abstract class. Only an abstract class can contain abstract methods.

Abstract classes are useful when you do not want objects created from the class. Declare a class as abstract when you want to use subclasses to complete the implementation.

When any non-abstract class derives from an abstract class through inheritance, the inherited class must implement all the abstract members of the abstract class. 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 using the syntax:

self::memberName

The optional SHARING PARENT phrase in the nested class definition enables 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 using the self:: syntax, 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 self::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 cannot access instance members of the containing class with the self:: syntax, but only do it using an explicit object reference.

Previous Topic Next topic Print topic