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.

10 comments:

yektek training said...

Very nice information given by you thank you

Paul Karunakar said...

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

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

Reddy sap installation said...
This comment has been removed by the author.
girish shinde said...

simple to understand

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

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!!!!!!

Osman Pasha said...

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

Keerthi Suresh said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-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!!!!!!

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD