Created On: 16 June 2011
Problem:
What are abstract classes in managed Visual COBOL and how do I use them?
Resolution:
Abstract classes are used for creating base OO classes that contain both Abstract methods and implemented methods.
Abstract classes cannot be instantiated directly but instead they must be inherited by a derived class in order to be used.
The Abstract methods defined must be implemented by the derived class much like in an Interface.
Methods that have been implemented in the Abstract class itself can be called directly when the derived class is instantiated.
Example:
Abstract classes cannot be instantiated directly but instead they must be inherited by a derived class in order to be used.
The Abstract methods defined must be implemented by the derived class much like in an Interface.
Methods that have been implemented in the Abstract class itself can be called directly when the derived class is instantiated.
Example:
program-id. Main. data division. working-storage section. 01 calculate type DerivedClass. 01 added binary-long. 01 multiplied binary-long. procedure division. set calculate to new DerivedClass set added to calculate::AddTwoNumbers(10, 20) set multiplied to calculate::MultiplyTwoNumbers(10, 20) display "Added = " added display "Multiplied = " multiplied set calculate::groupProperty to "1234567890" goback. end program Main. class-id LRMClasses.absClass1 as "absClass1" abstract . working-storage section. *> A Non abstract method method-id AddTwoNumbers. local-storage section. procedure division using by value Num1 as binary-long by value Num2 as binary-long returning result as binary-long. compute result = Num1 Num2 goback. end method. *> An abstract method, to be *> overridden in derived class method-id MultiplyTwoNumbers abstract. local-storage section. procedure division using by value Num1 as binary-long by value Num2 as binary-long returning result as binary-long. end method. end class. class-id DerivedClass inherits from "absClass1". working-storage section. 01 groupProperty property. 05 prop1 pic x(5). 05 prop2 pic x(5). method-id MultiplyTwoNumbers override. local-storage section. procedure division using by value Num1 as binary-long by value Num2 as binary-long returning result as binary-long. compute result = Num1 * Num2 goback. end method. end class.
Old KB# 34174