Monday, February 11, 2013

Public, Protect and Private access in OOPs

Class components will have to be declared under any of visibility sections like Public Section, Protected Section and Private Section. These visibility sections will have to be declared with this order. That means protected section or Private section cannot come before Public section. As per the requirement if the program only needs to contain Public section then there is no need to declare further section. This is similar to the Protected and Private section also.

The data and methods declared in Public Section in a class can be and accessed by that class and any user, subclass of the program.
When the data and methods are declared in Protected Section in a class then those will be accessed by that class and sub-classes only. Sub-classes can be any child class of the main class.
When the data and methods are declared in Private Section in a class then those will be accessed by only that class, not by any other class.

Below is a program by which these 3 condition can be reflected:

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
  PUBLIC SECTION.
    DATA v_pub_parent TYPE char50 VALUE 'Parent class public data'.
    METHODS m_parent.

  PROTECTED SECTION.
    DATA v_pro_parent TYPE char50 VALUE 'Parent class protected data'.

  PRIVATE SECTION.
    DATA v_pri_parent TYPE char50 VALUE 'Parent class private data'.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS m_child.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD m_parent.
    WRITE: / 'Accessing components from Parent class',
           / v_pub_parent,
           / v_pro_parent,
           / v_pri_parent.
    SKIP.
  ENDMETHOD.                    "m_parent
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD m_child.
    WRITE: / 'Accessing components from Child class',
           / v_pub_parent,
           / v_pro_parent.
    SKIP.
  ENDMETHOD.                    "m_child
ENDCLASS.                    "child IMPLEMENTATION

DATA: obj_parent TYPE REF TO parent,
      obj_child  TYPE REF TO child.

START-OF-SELECTION.
  CREATE OBJECT: obj_parent, obj_child.

  CALL METHOD: obj_parent->m_parent,
               obj_child->m_child.

  WRITE: / 'Every user can access public data',
         / obj_parent->v_pub_parent.

Now at debugging level we shall see the accessing components of same class. Public, protected & private components are accessible from the same class.


From the child / sub class public & protected components are accessible.


From the user level only the public components are accessible.


Below is the output of this.

5 comments:

Unknown said...

I salute u man for explaining in a very simple and perfect way. thank you.

Anonymous said...
This comment has been removed by the author.
girish shinde said...

simple to understand

Unknown said...

Best SAP Success Factors Training Institute in Chennai
Best SAP MM Training in Chennai
Best SAP SD Training in Chennai
Best SAP ABAP Training in Chennai
Best SAP FICO Training in Chennai
Best SAP BASIS Training in Chennai

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
http://thecreatingexperts.com/sap-mm-training-in-chennai/
http://thecreatingexperts.com/sap-sd-training-in-chennai/
http://thecreatingexperts.com/sap-hr-training-in-chennai/
http://thecreatingexperts.com/sap-fico-training-in-chennai/
http://thecreatingexperts.com/sap-abap-training-in-chennai/
http://thecreatingexperts.com/sap-basis-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

Unknown said...

Very Useful Blogs and it is very easy to understand thanks for sharing.