Access data members and properties
- Last Updated: June 1, 2021
- 3 minute read
- OpenEdge
- Version 13.0
- Documentation
You can directly access data members and properties from within the class
where they are defined by using the data member or property name anywhere you can use a
variable. This includes all data members or properties defined directly within the class
definition and all PUBLIC,PACKAGE-PROTECTED, PROTECTED, and PACKAGE-PRIVATE data members or properties defined in any super
class of the class hierarchy. PACKAGE-PRIVATE data members
and properties which are defined in any super class of the class hierarchy can be accessed
directly if the defining class and the accessing class are in the same package. For
properties, access also depends on whether they are defined as readable or writable, or
both.
You can access a PUBLIC instance data member
or property from outside the class hierarchy of an object where it is defined by using an
object reference to qualify the data member or property name. Also, note that only variable
data members and properties can be PUBLIC.
While you can also define non-public properties, a primary benefit of
properties is to encapsulate non-public data members. Thus, because properties can be defined
for many variable data types, PUBLIC properties are
well-suited for encapsulating variable data members.
There is no direct access to PRIVATE or
PROTECTED data members or properties from outside the class
hierarchy, although an instance can access a PRIVATE member
of another instance if they are the same class, and an instance can access the PROTECTED member of a second instance that is at the same level or
higher in the class hierarchy. The following code demonstrates how an instance can access the
PRIVATE data member of another instance of the same
class.
|
|
The constructor of PrivateNumber takes an
integer as input, which it then assigns to the PRIVATE data
member iMyNumber. The ShowNumbers method takes an object as input and then displays iMyNumber for both the current instance and the instance passed to
it. Because instances of the same class can access each other’s private data members, the
invocation of ShowNumbers for InstanceOne with InstanceTwo as a parameter will
display the private data members of both instances.
If you want to expose a non-variable (such as a buffer, temp-table, query, or ProDataSet) to other classes, you must do so using one of these mechanisms:
- Implement
PUBLICmethods that pass the data object as a parameter. - Define a
PUBLIC HANDLEvariable, data member, or property that allows access to the data object through its handle. Note that handle data members inherently undermine encapsulation because they provide direct access to the data objects they reference. To encapsulate access to all non-variable data members, including data objects, define non-PUBLICand static versions of these data members and pass them as parameters (if possible) toPUBLICmethods of the class.Note: Some data objects, such as query objects, can be defined as static data objects, but can only be passed as parameters using their handles.