4. Working with Classes : How You Can Create a User Class : How You Can Encapsulate Attributes and Methods
 
Share this page                  
How You Can Encapsulate Attributes and Methods
In object‑oriented programming, you can limit access to an object's attributes and methods. The process of allowing only the object itself to have access to its attributes and methods is called encapsulation. In OpenROAD, you accomplish this restriction by flagging an attribute or method as private. You can only reference a private attribute or method in the 4GL code of the methods of the defining user class or its subclasses.
Attribute encapsulation is useful when you have an attribute that is affected by several different methods, each of which needs that attribute to be in a known state. To prevent programmers from changing the attribute in the 4GL code, you can make the attribute private so that programmers can only access it from the object's methods.
Method encapsulation is useful if you want a method to be invoked only by another method of the user class or its subclasses. For example, if you have a function that you would usually define as a local procedure, but you want child objects to have access to the function as well, you would code the function as a private method rather than a local procedure, thus overcoming the scoping limitations of local procedures. If you defined the function as a local procedure, you would have to redefine the procedure in every relevant method.
When you create a user class as an abstract class, you can keep others from using this class by making all of its attributes and methods private. Caution is required, however, when flagging everything in an abstract class as private. Private classes deny access to developers who are using objects that inherit features of the abstract class.