Try OpenEdge Now
skip to main content
Object-oriented Programming
Getting Started with Classes, Interfaces, and Objects : Using the CLASS construct : Defining data members within a class : Comparison with procedure-based programming
 
Comparison with procedure-based programming
The variables and other data elements defined in the main block of an external procedure are private without having an explicit access mode. That is, by default, they are scoped to and accessible from elements of the procedure, including any of its internal procedures or user-defined functions, and they cannot be directly accessed from other external procedures. In the same way, data members defined with no access mode in a class are PRIVATE and accessible from all methods defined in the class; and they cannot be directly accessed from other classes, whether inside or outside of the class hierarchy, or from external procedures.
Procedures can define SHARED data elements, which are scoped to and accessible from other external procedures. For SHARED data elements, the data definitions must be defined as NEW SHARED in one external procedure and these same data definitions must be repeated as SHARED in each called external procedure that accesses them. This is necessary because the compiler does not look at other external procedures when compiling a procedure.
Classes do not support SHARED data elements. However, SHARED data elements in procedures roughly correspond to PROTECTED and PUBLIC data members in classes. PROTECTED and PUBLIC data members in classes provide wider access without the need to repeat the data member definitions as is required for SHARED data elements in procedures. However, data members must be referenced with respect to the instantiated object where they are defined. So, PROTECTED data members can only be referenced within the same class hierarchy of a single instantiated object where they are defined; and PUBLIC data members can be referenced like PROTECTED data members, within the class hierarchy, and also from outside the class hierarchy. However, PUBLIC data members can only be referenced outside the class hierarchy where they are defined by using a qualifying prefix associated with the defining class. For an instance data member, the qualifying prefix is an object reference to an instance of the defining class, and for a static data member, the qualifying prefix is the type name of the defining class. These referential restrictions both identify class and object relationships to the data members and allow the compiler to validate data member references for consistency based on these relationships.