07-16-2008 9:32 AM
I need a scenario where its mandatory to use
1) static variables in the class
2) protected section of the class
3) public section
for example take the case of private section
example bank as a class
let it have data : account balance
method : manager
they are mandatory to put above as private so as other will not access
regards
Edited by: abap learner on Jul 16, 2008 10:32 AM
07-16-2008 1:47 PM
Here's a scenario that needs some of those points.
Implement a class that can only have one instance created. ( I.e. implement the singleton pattern ).
Hint.
You need a static attribute so_ref which is a reference to that class.
You need a static method that returns a reference to that class. This method sees whether so_ref is bound. If it is, returns so_ref. If it isn't CREATE OBJECT so_ref, and returns so_ref.
The class must be set for private instantiation.
07-16-2008 9:35 AM
hi,
a> Static variable is required to count the no objects of spesific
class.
b> In case of protected section, let us consider that you are working with a code deals with Car Manufacturer. Their product are all car.. so they have some common character and if we segregate the cars in BUS,TAXI etc. , they have these common attributes as well as some features of their own. In this cases we use inheritence where Protected section is essential.
d> we use public section for interface and generaly store method there.
Regards,
anirban
07-16-2008 10:02 AM
> Static variable is required to count the no objects of spesific class
How could this happen
plz explain
07-16-2008 11:20 AM
Hi,
Actualy in ABAP we do not have any direct mechanism to calculate the no of object of a class. So declare a Static variable to store the number.
and we call it afetr every CREATE statement when an object is created.
Chaeck the code, you will get the idea,
REPORT z_oops.
" Class cls definition----
CLASS cls DEFINITION.
PUBLIC SECTION.
METHODS:
add IMPORTING
im_var1 TYPE i
im_var2 TYPE i,
display.
CLASS-METHODS:
display1 RETURNING
value(re_count) TYPE i.
PRIVATE SECTION.
DATA:
w_var1 TYPE i,
w_var2 TYPE i,
w_result TYPE i.
CLASS-DATA:
w_count TYPE i .
ENDCLASS. "cls DEFINITION
" Class cls implementation----
CLASS cls IMPLEMENTATION.
METHOD add.
w_var1 = im_var1.
w_var2 = im_var2.
w_result = im_var1 + im_var2.
ENDMETHOD. "add
METHOD display.
WRITE:
/ w_var1,'+',w_var2,'=',w_result.
ENDMETHOD. "display
METHOD display1.
WRITE: /'welcome to class method.'.
ADD 1 TO w_count.
re_count = w_count.
WRITE: 'no of object = ',w_count.
ENDMETHOD. "display1
ENDCLASS. "cls IMPLEMENTATION
*" Data declaration
PARAMETERS:
p_var1 TYPE i,
p_var2 TYPE i.
DATA:
ref1 TYPE REF TO cls,
ref2 TYPE REF TO cls,
ref3 TYPE REF TO cls.
START-OF-SELECTION.
CREATE OBJECT ref1.
cls=>display1( ).
CREATE OBJECT ref2.
cls=>display1( ).
CREATE OBJECT ref3.
cls=>display1( ).
ref1->add( im_var1 = p_var1
im_var2 = p_var2 ).
Regards,
anirban
07-16-2008 1:47 PM
Here's a scenario that needs some of those points.
Implement a class that can only have one instance created. ( I.e. implement the singleton pattern ).
Hint.
You need a static attribute so_ref which is a reference to that class.
You need a static method that returns a reference to that class. This method sees whether so_ref is bound. If it is, returns so_ref. If it isn't CREATE OBJECT so_ref, and returns so_ref.
The class must be set for private instantiation.
07-16-2008 2:16 PM
Hi,
Static variable : for ex. you need to calculate no. of hits to your website. You need one variable it should be unique for all the instances. if suppose second hits the website he should read the value from static variable not from the instance variable. Instance variable will be maintaining data only for that instance not for the whole class.
Protected section : You have some secure method in your class which are using some secured information and you dont want to give access to any body like sending your bank details or accessing your bank account details those type of functions you can declare as protected. with in the class you can use not from outside the class.
Public: Is public any one can access..
Reward if it is helpfull <= read [the rules|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/home/rulesofEngagement] here! Points removed.
Regards,
Naresh.
Edited by: Julius Bussche on Jul 17, 2008 12:18 PM
07-17-2008 1:12 PM