Mentor SAP

Definition of Local Classes

The concept of classes is the foundation of object-oriented programming. A class can either have public components or private components. You can access public components, such as methods and events, from outside a class. However, you cannot access private components, for example, data types and attributes, from outside a class.

 

The figure shows a vehicle as an example of a class.

 

Characteristics of a class in object-oriented programming are as follows:

 

 

 Declaration of Attributes

Attributes contain the data that can be stored in the objects of a class.The types of attributes are as follows:

 

Attributes can consist of data types (local or global) or reference types.

 

 

Definition of Attributes, Types, and Constants

In class DATA statements, you can only use the TYPE addition to refer to data types.

The LIKE addition is only allowed for local data objects or SY fields (for example, SY-DATE, SY-UNAME, and so on).

 

The READ-ONLY addition indicates that a public attribute declared with DATA can be read from outside. However, the attribute can only be changed by methods in the same class. Currently, you can use the READ-ONLY addition in the public visibility section (PUBLIC SECTION) of a class declaration or in an interface definition.

 

With TYPE REF TO, you can type an attribute as a reference.

The CONSTANTS statement is used within the class definition to define data objects that have a constant value.

 

If you use the TYPES statement in the class definition, you are declaring a local type, which is specific to this local class. You can create a local type to be used by one or more attributes within the same class.

 

 

Visibility of Attributes

Private attributes cannot be addressed directly from outside the class and are not visible to outside users. You can protect attributes from outside access by characterizing them as private attributes.

 

The friendship concept is an exception to this rule.

 

Public attributes are attributes that are visible to all users and can be directly accessed by outside users.

 

Similarly, the constants and types defined by a class can be either private (for use inside the class), or the public, (accessible from outside the class). The public components of a class are collectively known as the class interface.

 

Using the private visibility section is known as information hiding or encapsulation. Encapsulation protects a class user by making changes to private components invisible to the external user.

 

You can change the private components of a class without changing the interface. Assume that the private components of a class are changed at some point with its interface remaining the same, external users can still continue to work with the class as before. The external users can only access the components through the interface of the class and will not notice the internal implementation changes in the PRIVATE SECTIONS. However, when the public components of a class change, every external user must take those changes into account. Therefore, use public attributes sparingly, or avoid making incompatible changes to public components altogether.

 

 

Accessing Private Attributes