Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

private

Former Member
0 Likes
531

Hi all

Can anybody explain me private methods r attributes..why do we need to hide information for outside user.iam not able to understand this concept..please help me with any examples if possible..thanks

Regards

vij

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
496

Hi Vijaya,

using the private visibility section, you can change superclasses without the

need to know the subclasses. As long as the changes you make do not affect the

semantics, you do not need to adapt the subclasses. This is because they only

indirectly access the private components from the superclass.

Regards, Peter

Example for accesing private method:

CLASS lcl_vessel DEFINITION.

PUBLIC SECTION.

METHODS set_type IMPORTING

im_name TYPE string

im_model TYPE string.

PRIVATE SECTION.

METHODS init_type.

DATA:

name TYPE string,

model TYPE string.

ENDCLASS.

CLASS lcl_vessel IMPLEMENTATION.

METHOD init_type.

CLEAR: name, model.

ENDMETHOD.

METHOD set_type.

IF im_name IS INITIAL.

  • Calling method init_type...

ELSE.

name = im_name.

model = im_model.

ENDIF.

ENDMETHOD.

ENDCLASS.

4 REPLIES 4
Read only

Former Member
0 Likes
497

Hi Vijaya,

using the private visibility section, you can change superclasses without the

need to know the subclasses. As long as the changes you make do not affect the

semantics, you do not need to adapt the subclasses. This is because they only

indirectly access the private components from the superclass.

Regards, Peter

Example for accesing private method:

CLASS lcl_vessel DEFINITION.

PUBLIC SECTION.

METHODS set_type IMPORTING

im_name TYPE string

im_model TYPE string.

PRIVATE SECTION.

METHODS init_type.

DATA:

name TYPE string,

model TYPE string.

ENDCLASS.

CLASS lcl_vessel IMPLEMENTATION.

METHOD init_type.

CLEAR: name, model.

ENDMETHOD.

METHOD set_type.

IF im_name IS INITIAL.

  • Calling method init_type...

ELSE.

name = im_name.

model = im_model.

ENDIF.

ENDMETHOD.

ENDCLASS.

Read only

thomasalexander_ritter
Product and Topic Expert
Product and Topic Expert
0 Likes
496

Imagine you are writing a class but there are some parts which you might want or need to change later. You have to make sure to tell other developers which methods, attributes might change. Basically by marking an attribute or a method as public you make a contract with the other developers and say "Hey, you can use this public methods, attributes safely I will never change them".

An example. You have to build a class which allows you to create a salesorder. You write one public method create_salesorder(). Now, you realize that the method contains too much code and you want to split it up. In the end you would mark only the create_salesorder() public and the helper methods private:

public create_salesorder()

private helper_one()

private helper_two()

In order to get a feeling for what should be private take a look at some classes from SAP = Learn from the pros!

cheers

Thomas

Read only

uwe_schieferstein
Active Contributor
0 Likes
496

Hello Vijaya

Recently I developed a report for a customer used for CATS cost distribution. The customer selects a time range of CATS data of his employees, the data are aggregated according to different criteria and finally the sum values are used to update assessment cycles (transaction KSU2).

One important requirement was that under certain conditions CATS records should be excluded from the cost distribution. For this pupose I created a specific class dealing with these exceptions. The class had the following methods:

SET_SPECIAL_EMPLOYEE_RANGE	
              Instance Method      Private
              fill range with special employee numbers
CONSTRUCTOR	
              Instance Method      Public
SELECT_CATS_EXCEPTIONS	
              Instance Method      Private
              select exceptions for CATS cost distribution
IS_IGNORED	
              Instance Method      Public
              check whether a CATS record should be ignored

The model class holds all the selected CATS data and is responsible for the calculations. Now when the model class wants to know whether a CATS record should be excluded from the calulations or not it simply calls method IS_IGONRED of the exception class:

*   Check if an exception is defined for employee / date / receiver
    CALL METHOD me->mo_catsdb_cx->is_ignored
      EXPORTING
        id_pernr      = ls_calc2-pernr
        id_date       = ls_calc2-workdate
        id_rcvrobj    = ld_receiver_obj
      RECEIVING
        rd_is_ignored = lx_is_ignored.

    IF ( lx_is_ignored = abap_true ).
...

The model class has no clue whatsoever why a CATS record is excluded or not. Even more important, the model class need not to know anything about this.

If the customer changes the criteria for excluding CATS records what happens to the model class? Nothing because this logic will be hidden wihin private methods of the exception class. The model class only "sees" the public method IS_IGNORED which does not change at all (at least the interface does not change).

When I create classes the majority of the methods are private.

Regards

Uwe

Read only

Former Member
0 Likes
496

Hi Vijaya,

Public components of a class can be accessed directly by using an object of the class, where as private components can't be directly accessed, they can be accessed using public components of the class.

For encapsulation we make attributes private and methods public.

Think of an application where an user needs to provide a password and press a button to get some records. When the user presses the button, a validating code checks if the user has at all provided the password and whether value provided is consistent. If that validating code was not there, there would have been a risk that user even without providing a password (null value) or providing an inconsistent value, would have fetched some records.

Relating this with private attributes and public methods, we can say that by making data as private, we eliminate the risk that a user directly access it and set it incorrectly. Only through validating public mehtods private data can be set.

Award points if found useful.

Regards

Indrajit