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

Static and instance method or attribute

Former Member
0 Likes
1,656

Hi Guys,

Can any body tell me with example at which scenario i can use static and instance method or attribute and Please tell about polymorphism concept in ABAP Objects with example.

Edited by: Ragu Prasad on Jun 23, 2008 6:20 AM

6 REPLIES 6
Read only

Former Member
0 Likes
919

This message was moderated.

Read only

matt
Active Contributor
0 Likes
919

Or you can look at the original fine article here: http://beingkedar.googlepages.com/ooabapo.doc

matt

Read only

Former Member
0 Likes
919

Hi,

Static/Instance Method:

CLASS_CONSTRUCTOR is an example of a static method

CONSTRUCTOR is an example of instance method.

When you want to have implementation of a method that can be called without the object of a class

the such methods are static. Static are basically used in util class. For example CL_HTTP_SERVER method ESCAPE_URL

When you want to have implementation of a method that can be called with each instance of the class and the processing depends on some inputs then such are instance methods. For example in Class Car you can have a method FUEL_LEVEL. This method will give different ouput for different instances of the same class.

Static/Instance attribute:

Attribute that are at class level (commom to all objects for example class_name) are static

Attributes specific to an instance like in the above example color of the car.

Polymorphism:

There are 2 types of polymorphism:

1. Compile time (early binding) : Function overloading -


> not supported in abap

2. Run time (late binding) : Function overriding ---> works well in ABAP.

I hope this gives you some idea.

Regards,

Saurabh

Read only

Former Member
0 Likes
919

Hiii!

Just try out this program. Open this program in debugging mode. Your doubts will be cleared. In this program, You can see I have used one internal table to store references to classes. In the LOOP-ENDLOOP statement I'm assigning the references of lcl_passenger and lcl_cargo to their superclass reference. thats where the polymorphism lies. In polymorphism,

Objects from different classes behave differently to same method call. try this example in debugging mode.

Now static or class methods, exist once per class.

While the instance attribute exist once per object or instance

REPORT YH1146_CLASS_CASTING.

TYPE-POOLS icon.

DATA: r_plane TYPE REF TO lcl_airplane,

r_cargo TYPE REF TO lcl_cargo_plane,

r_passenger TYPE REF TO lcl_passenger_plane,

plane_list TYPE TABLE OF REF TO lcl_airplane.

START-OF-SELECTION.

lcl_airplane=>display_n_o_airplanes( ).

CREATE OBJECT r_passenger EXPORTING

im_name = 'LH BERLIN'

im_planetype = '747-400'

im_seats = 345.

APPEND r_passenger TO plane_list.

CREATE OBJECT r_cargo EXPORTING

im_name = 'US Hercules'

im_planetype = '747-500'

im_cargo = 533.

APPEND r_cargo TO plane_list.

LOOP AT plane_list INTO r_plane.

r_plane->display_attributes( ).

ENDLOOP.

lcl_airplane=>display_n_o_airplanes( ).

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

CONSTANTS: pos_1 TYPE i VALUE 30.

METHODS:

constructor IMPORTING

im_name TYPE string

im_planetype TYPE saplane-planetype,

display_attributes.

CLASS-METHODS: display_n_o_airplanes.

PRIVATE SECTION.

METHODS: get_technical_attributes

IMPORTING im_type TYPE saplane-planetype

EXPORTING ex_weight TYPE s_plan_wei

ex_tankcap TYPE s_capacity.

DATA: name TYPE string,

planetype TYPE saplane-planetype.

CLASS-DATA: n_o_airplanes TYPE i.

ENDCLASS. "lcl_airplane DEFINITION

----


  • CLASS lcl_airplane IMPLEMENTATION

----


*

----


CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

name = im_name.

planetype = im_planetype.

n_o_airplanes = n_o_airplanes + 1.

ENDMETHOD. "constructor

METHOD display_attributes.

DATA: weight TYPE saplane-weight,

cap TYPE saplane-tankcap.

WRITE: / icon_ws_plane AS ICON,

/ 'Name of airplane', AT pos_1 name,

/ 'Airplane type:', AT pos_1 planetype.

get_technical_attributes( EXPORTING im_type = planetype

IMPORTING ex_weight = weight

ex_tankcap = cap ).

WRITE: / 'Weight:', weight,

'Tank capacity:', cap.

ENDMETHOD. "display_attributes

METHOD display_n_o_airplanes.

WRITE: /,/ 'Number of airplanes:',AT pos_1 n_o_airplanes

LEFT-JUSTIFIED, /.

ENDMETHOD. "display_n_o_airplanes

METHOD get_technical_attributes.

SELECT SINGLE weight

tankcap

FROM saplane

INTO (ex_weight, ex_tankcap)

WHERE planetype = im_type.

IF sy-subrc <> 0.

ex_weight = 100000.

ex_tankcap = 10000.

ENDIF.

ENDMETHOD. "get_technical_attributes

ENDCLASS. "lcl_airplane IMPLEMENTATION

----


  • CLASS lcl_cargo_plane DEFINITION

----


*

----


CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.

PUBLIC SECTION.

METHODS: constructor IMPORTING im_name TYPE string

im_planetype TYPE saplane-planetype

im_cargo TYPE scplane-cargomax.

METHODS: display_attributes REDEFINITION.

METHODS: get_cargo RETURNING value(re_cargo) TYPE scplane-cargomax.

PRIVATE SECTION.

DATA: max_cargo TYPE scplane-cargomax.

ENDCLASS. "lcl_cargo_plane DEFINITION

----


  • CLASS lcl_cargo_plane IMPLEMENTATION

----


*

----


CLASS lcl_cargo_plane IMPLEMENTATION.

METHOD constructor.

CALL METHOD super->constructor( im_name = im_name

im_planetype = im_planetype ).

max_cargo = im_cargo.

ENDMETHOD. "constructor

METHOD display_attributes.

super->display_attributes( ).

WRITE: / 'Max Cargo = ', max_cargo.

ULINE.

ENDMETHOD. "display_attributes

METHOD get_cargo.

re_cargo = max_cargo.

ENDMETHOD. "get_cargo

ENDCLASS. "lcl_cargo_plane IMPLEMENTATION

----


  • CLASS lcl_passenger_plane DEFINITION

----


*

----


CLASS lcl_passenger_plane DEFINITION INHERITING FROM lcl_airplane.

PUBLIC SECTION.

METHODS: constructor IMPORTING im_name TYPE string

im_planetype TYPE saplane-planetype

im_seats TYPE sflight-seatsmax.

METHODS: display_attributes REDEFINITION.

PRIVATE SECTION.

DATA: max_seats TYPE sflight-seatsmax.

ENDCLASS. "lcl_passenger_plane DEFINITION

----


  • CLASS lcl_passenger_plane IMPLEMENTATION

----


*

----


CLASS lcl_passenger_plane IMPLEMENTATION.

METHOD constructor.

CALL METHOD super->constructor( EXPORTING im_name = im_name

im_planetype = im_planetype ).

max_seats = im_seats.

ENDMETHOD. "constructor

METHOD display_attributes.

super->display_attributes( ).

WRITE: / 'Max Seats = ', max_seats.

ULINE.

ENDMETHOD. "display_attributes

ENDCLASS. "lcl_passenger_plane IMPLEMENTATION

Edited by: Abhijeet Kulshreshtha on Jun 24, 2008 10:25 AM

Edited by: Abhijeet Kulshreshtha on Jun 24, 2008 10:32 AM

Read only

Former Member
0 Likes
919

Hi Ragu,

To the first part of your question, the answer is,

we use instance method to represent different element in an object of a class and

to manipulate them we use various instance methods .

But, if you have any global scenario that is applicable to the whole class rather on a

specific object of the class we use static attributes and to manipulate them ,we use static methods

You can check the following code.

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 created= ',re_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 re31.

cls=>display1( ).

ref1->add( im_var1 = p_var1

im_var2 = p_var2 ).

ref1->display( ).

Here, add and display are the instance methods and are triggered by the object of the class.

However,in OO concept of ABAP, there is no mechanism to calculate the no. of object created in a class.

Since, no. of object is to be calculated in the global level, you can manipulate it by using static data w_count and static function display1( ) and call it after creating each object.

i am working with Polymorphism with simple code now. Will get to you soon

Reward if helpful.

All the best.

Anirban Bhattacharjee

Read only

Former Member
0 Likes
919

hi,

welcome to SDN.

Its not at all permitted to post code on SDN.

You can check these links to further strenthen your concept.

[http://help.sap.com/saphelp_nw70/helpdata/EN/c3/225b5c54f411d194a60000e8353423/content.htm]

hope this will help.

Reward if useful.

Sumit Agarwal