Class Visibility

 Introduction:   

  • We will cover the following basic concepts:
  1. Simple example class, specially designed to understand the basics of Class Visibility.
  2. How visibility of the class components like attributes and methods work.
  3. How can a developer utilize the visibility property to his advantage ?. Use of 'Me' variable
  4. Demonstration of visibility in an executable eclipse program.
  5. Why use Visibility?
 Lets Start:

1. Let us create a class in HANA Eclipse with the following structure.
  • We will define the below components in the class definition.
  • PUBLIC SECTION:
    • This means the class components like Attributes and Methods defined in this section are visible outside the Class, in Sub-Classes, in an Executable Program.
      • We will create Attribute: CV_PUBLIC_ATTR with default value 'PUBLIC SECTION'.
      • We will create Method: CM_PUBLIC_METHOD,CM_PUBLIC_MAIN_METHOD.
  • PROTECTED SECTION:
    • This means the class components like Attributes and Methods defined in this section are visible only to their Classes and Sub-Classes.
      • We will create Attribute: CV_PROTECTED_ATTR with default value 'PROTECTED SECTION'.
      • We will create Method: CM_PROTECTED_METHOD.
  • PRIVATE SECTION:
    • This means the class components like Attributes and Methods defined in this section are visible only to their Class.
      • We will create Attribute: CV_PRIVATE_ATTR with default value 'PRIVATE SECTION'.
      • We will create Method: CM_PRIVATE_METHOD.

2. Let us, create the Implementation of the Class.
  • In the implementation section of the class, we will implement the 3 methods with different visibility as shown below.
    • CM_PUBLIC_METHOD Method Implementation. WRITE public attribute CV_PUBLIC_ATTR.
    • CM_PROTECTED_METHOD Method Implementation. WRITE protected attribute CV_PROTECTED_ATTR.
    • CM_PRIVATE_METHOD Method Implementation. WRITE private attribute CV_PRIVATE_ATTR.


3. Special Method Implementation.
  • We call this method special because we will make use of the self-reference variable called ME.
  • ME variable is a local reference variable.
  • This variable is automatically available, which points to the instance we created while calling the method of the class.
  • Using this reference variable, we can call all the class instance methods.
  • Methods or Attributes of any visibility that belongs to the class can be called using the ME variable.
  • So exactly this is what we are going to do. As shown below, we will call the methods of all the visibilities in the Method CM_PUBLIC_MAIN_METHOD.



4. Let us, create a program to call and demonstrate the visibility of the Class.
  • Now let us call all the methods in the program with the object created from the class reference parameter LR_CLASS.

  • As we can see below we get a syntax error for methods with visibility PROTECTED and PRIVATE.
  • Because the visibility is not public we will not be able to call the methods directly in an executable program.

  • So let us comment: protected and private method calls as shown below. 

  • Now let us call the Special Public Method that enables us to access the content of our protected and private methods.
  • Save and Activate the program.

  • Now let us see the output by running the ABAP Console.
    • Run->Run As->ABAP Application( Console ).



5. So Why Visibility?
  • As we have seen the power of visibility, the developer of the class can decide on the components of the class to be directly visible outside the class or not.
  • The visibility acts as an abstraction layer while we try to access the components of the class.
  • What this does is give the developer control to secure the class's components based on a need-to-need basis.
  • In our case we have somehow extracted the data from the PRIVATE and PROTECTED Methods using our Special Public Method CM_PUBLIC_MAIN_METHOD.
  • We call it special because as a developer we have made a conscious decision to let the outside world ( executable program in our case) access the PRIVATE and PROTECTED Methods content.
  • This is done to demonstrate how a Self Reference Variable ME works.
  • But in a Real Word scenario, the Owner/Developer of the class can restrict access to the component of the class by working with Visibility.
Example Code:
REPORT zclass_visibility_pgm.
*Let us call the methods of all the visibility
*Create Class Reference Variable and Object
DATA(lr_class) = NEW zclass_visibility( ).

*Call the Public Method
WRITE: 'Public Method Call:'.
WRITE: /.
lr_class->cm_public_method( ).

*Call the Protected Method
*lr_class->cm_protected_method( ).

*Call the Private Method
*lr_class->cm_private_method( ).

*Call Main Public Method
WRITE: /.
WRITE: / 'Call All the Visibility Methods using Main Public Method:'.
WRITE: /.
lr_class->cm_public_main_method( ).


Comments

Popular posts from this blog

Special Method: Constructor

Class Inheritance