An important feature of object-orientation is the encapsulation of attributes and methods - ultimately of functionality - in classes. A class guarantees its user specific properties and specific behavior. The sum of these properties is called the class interface. The Visibility mechanism defines the class interface which is available to the users.
There are three commonly defined types of visibility in object-oriented technology:
Public
The relevant class component (attribute, method, event etc.) is visible to all classes.
The relevant class component (attribute, method, event etc.) is visible to all classes.
Protected
The relevant class component (attribute, method, event etc.) is visible to the class itself and all inheritors. (We will return to the terms Inheritor and Inheritance later in this document.)
The relevant class component (attribute, method, event etc.) is visible to the class itself and all inheritors. (We will return to the terms Inheritor and Inheritance later in this document.)
Private
The relevant class component (attribute, method, event etc.) is only visible to the class itself.
The relevant class component (attribute, method, event etc.) is only visible to the class itself.
Code listing for: Z_005_VISIBILITY
Description: COMPONENT VISIBILITY
Description: COMPONENT VISIBILITY
*---------------------------------------------------------------
* In this example the PRIVATE attribute NAME is hidden from the
* users. Access to the information it contains is realized via
* the PUBLIC method SET_NAME & GET_NAME.
*---------------------------------------------------------------
*
* TYPES OF VISIBILITY :
* (1) PUBLIC : class component is visible to all classes.
* (2) PRIVATE : class component is visible to the class itself.
* (3) PROTECTED : class component is visible to class itself and
* all inheritors (sub class).
*
*---------------------------------------------------------------
REPORT Z_005_VISIBILITY.
TYPES: TY_NAME(50) TYPE C.
*----------------------------------------------------------------
* CLASS CL_NAME DEFINITION
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION.
PUBLIC SECTION.
METHODS: SET_NAME IMPORTING VALUE(IM_NAME) TYPE TY_NAME,
GET_NAME EXPORTING VALUE(EX_NAME) TYPE TY_NAME.
PRIVATE SECTION.
DATA NAME TYPE TY_NAME.
ENDCLASS. "CL_NAME DEFINITION
*----------------------------------------------------------------
* CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
METHOD SET_NAME .
NAME = IM_NAME.
ENDMETHOD. "SET_NAME
METHOD GET_NAME.
EX_NAME = NAME.
ENDMETHOD. "GET_NAME
ENDCLASS. "CL_NAME IMPLEMENTATION
START-OF-SELECTION.
" CREATE INSTANCE OF THIS CLASS, IF NO INSTANCE EXIST
DATA: INSTANCE TYPE REF TO CL_NAME,
NAME TYPE TY_NAME.
CREATE OBJECT INSTANCE.
" SET VALUE FOR NAME ATTRIBUTE
CALL METHOD INSTANCE->SET_NAME
EXPORTING
IM_NAME = 'VINO'.
" GET VALUE OF NAME ATTRIBUTE
CALL METHOD INSTANCE->GET_NAME
IMPORTING
EX_NAME = NAME.
WRITE: / 'NAME : ' , NAME.
" CLEAR MEMORY OCCUPIED BY OBJECTS
CLEAR: INSTANCE.
Program Output : 005
NAME : VINO
No comments:
Post a Comment