Defining a subclass
You can make a class (called a subclass, derived class, or child class) a specialization of another class (called a superclass, base class, or parent class).
A subclass inherits the methods and instance data of its superclasses, and is related to its superclasses by an is-a relationship. For example, if subclass P inherits from superclass Q, and subclass Q inherits from superclass S, then an instance of P is an instance of Q and also (by transitivity) an instance of S. An instance of P inherits the methods and data of Q and S.
Using subclasses has several advantages:
- Reuse of code: Through inheritance, a subclass can reuse methods that already exist in a superclass.
- Specialization: In a subclass you can add new methods to handle cases that the superclass does not handle. You can also add new data items that the superclass does not need.
- Change in action: A subclass can override a method that it inherits from a superclass by defining a method of the same signature as that in the superclass. When you override a method, you might make only a few minor changes or completely change what the method does.
Restriction: You cannot use multiple inheritance in your COBOL programs. Each COBOL class that you define must have exactly one immediate superclass that is implemented in Java™ or COBOL, and each class must be derived directly or indirectly from java.lang.Object. The semantics of inheritance are as defined by Java.
The structure and syntax
of a subclass definition are identical to those of a class definition:
Define instance data and methods in the DATA DIVISION
and PROCEDURE
DIVISION
, respectively, within the OBJECT
paragraph
of the subclass definition. In subclasses that require data and methods
that are to be associated with the subclass itself rather than with
individual object instances, define a separate DATA DIVISION
and PROCEDURE
DIVISION
within the FACTORY
paragraph of
the subclass definition.
COBOL instance data is private. A subclass can access the instance data of a COBOL superclass only if the superclass defines attribute (get or set) instance methods for doing so.
Defining a class
Overriding an instance method
Coding attribute (get and set) methods
Defining a subclass instance method
Defining a factory section
The Java Language Specification (Inheritance, overriding, and hiding)
COBOL class definition structure (Enterprise COBOL for z/OS® Language Reference)