‎2008 Aug 06 1:35 PM
Hi All,
Can someone give me a simple and precise example of Polymorphism in ABAP Objects through a program.
Thanks,
Ibrahim
‎2008 Aug 06 1:37 PM
With the upmost respect and politeness, seeing as you objected to my earlier replies to your posts, regarding reading the rules...
Have you tried using the search functionality. This topic has been covered time and time again.
‎2008 Aug 06 1:39 PM
‎2008 Aug 06 2:04 PM
>
> Yes i tried but i did not get proper example
I was surprised by this so I searched for "polymorphism example" in the forum and SDN search. I got hundreds of results. Doing same in the SAP help I also find a code example in less than 30 seconds. What kind of "proper example" do you need?
Please read the [Rules of Engagement|https://wiki.sdn.sap.com/wiki/display/HOME/RulesofEngagement], this is not the first time someone has reported your threads for abuse (and it wasn't Matthew).
If you struggle with some exapmple you find here or in the help we are glad to help with even with the most basic questions, but please respect other people's time who are not paid to help you here and make a little effort yourself before posting.
‎2008 Aug 06 1:40 PM
‎2008 Aug 06 1:41 PM
Hi,
check the below program for polymorphism in inheritance and using Redefinition statement.
ploymorphism means, method with the same name can behave differently for different class.
&----
*& Report ZB_TEST_POLYMORPHISM
*&
&----
*&
*&
&----
REPORT zb_test_polymorphism.
----
CLASS lcl_super DEFINITION
----
*
----
CLASS lcl_super DEFINITION.
PUBLIC SECTION.
METHODS: m1,
m2,
constructor.
PRIVATE SECTION.
DATA: v1 TYPE i.
ENDCLASS. "lcl_super DEFINITION
----
CLASS lcl_sub DEFINITION
----
*
----
CLASS lcl_sub DEFINITION INHERITING FROM lcl_super.
PUBLIC SECTION.
METHODS: m2 REDEFINITION,
constructor.
PRIVATE SECTION.
DATA: v1 TYPE i,
v2 TYPE i.
ENDCLASS. "lcl_sub DEFINITION
----
CLASS lcl_super IMPLEMENTATION
----
*
----
CLASS lcl_super IMPLEMENTATION.
METHOD m1.
WRITE: /, 'v1: ', v1.
ENDMETHOD. "m1
METHOD m2.
WRITE 'super'.
ENDMETHOD. "m2
METHOD constructor.
v1 = 1.
ENDMETHOD. "constructor
ENDCLASS. "lcl_super IMPLEMENTATION
----
CLASS lcl_sub IMPLEMENTATION
----
*
----
CLASS lcl_sub IMPLEMENTATION.
METHOD m2.
WRITE: 'sub',
/, 'v1: ', v1,
/, 'v2: ', v2.
CALL METHOD super->m2. "THIS WILL CALL M2 OF THE SUPER-CLASS
ENDMETHOD. "m2
METHOD constructor.
CALL METHOD super->constructor.
CALL METHOD m2. "THIS WILL CALL M2 OF THE SUB-CLASS
ENDMETHOD. "constructor
ENDCLASS. "lcl_sub IMPLEMENTATION
START-OF-SELECTION.
DATA: obj1 TYPE REF TO lcl_super,
obj2 TYPE REF TO lcl_sub.
CREATE OBJECT obj1.
CREATE OBJECT obj2.
CALL METHOD obj1->m2. "This will call M2 of the super class
obj1 = obj2.
CALL METHOD obj1->m2. "This will call M2 of the sub class
Regards,
Boobalan S
‎2008 Aug 06 1:44 PM
Boobalan,
What does REDEFINITION mean?
What will the statement obj1 = obj2 do?
Edited by: mohammed ibrahim on Aug 6, 2008 2:45 PM
‎2008 Aug 06 1:49 PM
Hii!
Redefinition means the implementation of an inherited insatnce method is changed for the subclass without changing the signature. At the same time , the visibility section for the superclass must remain the same.
Your another question, assignment of objects?
Suppose you have a superclass named lcl_vehicle whose runtime object is(instance) r_vehicle and its subclass is lcl_car whose runtime object(insatance) is r_car.
Now if you assign reference of class lcl_car to reference of class lcl_vehicle i.e r_vehicle = r_car.
Now with reference r_vehicle you can access the inherited components of subclass lcl_car. In ABAP OBJECTS this is called Narrow casting.
Regards
Abhijeet
‎2008 Aug 06 1:46 PM
Hii!
Polymorphism :- When Objects from different classes react differently to same method calls.
Check this sample code
Try this program in debugging mode.
REPORT z_CLASS_CASTING.
TYPE-POOLS icon.
INCLUDE z_class.
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( ).
INCLUDE PROGRAM
*&---------------------------------------------------------------------*
*& Include z_CLASS *
*&---------------------------------------------------------------------*
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
Regards
Abhijeet
Edited by: Abhijeet Kulshreshtha on Aug 6, 2008 2:59 PM
‎2008 Aug 06 2:07 PM
‎2008 Aug 07 11:08 AM
‎2008 Aug 11 4:56 AM
Hi,
please find the below example for Polymorphism....
REPORT zooabap_test4 .
----
CLASS lcl_super DEFINITION
----
*
----
CLASS lcl_super DEFINITION.
PUBLIC SECTION.
METHODS: m1,
m2,
constructor.
PRIVATE SECTION.
DATA: v1 TYPE i.
ENDCLASS. "lcl_super DEFINITION
----
CLASS lcl_sub DEFINITION
----
*
----
CLASS lcl_sub DEFINITION INHERITING FROM lcl_super.
PUBLIC SECTION.
METHODS: m2 REDEFINITION,
constructor.
PRIVATE SECTION.
DATA: v1 TYPE i,
v2 TYPE i.
ENDCLASS. "lcl_sub DEFINITION
----
CLASS lcl_super IMPLEMENTATION
----
*
----
CLASS lcl_super IMPLEMENTATION.
METHOD m1.
WRITE: /, 'v1: ', v1.
ENDMETHOD. "m1
METHOD m2.
WRITE 'super'.
ENDMETHOD. "m2
METHOD constructor.
v1 = 1.
ENDMETHOD. "constructor
ENDCLASS. "lcl_super IMPLEMENTATION
----
CLASS lcl_sub IMPLEMENTATION
----
*
----
CLASS lcl_sub IMPLEMENTATION.
METHOD m2.
WRITE: 'sub',
/, 'v1: ', v1,
/, 'v2: ', v2.
CALL METHOD super->m2. "THIS WILL CALL M2 OF THE SUPER-CLASS
ENDMETHOD. "m2
METHOD constructor.
CALL METHOD super->constructor.
CALL METHOD m2. "THIS WILL CALL M2 OF THE SUB-CLASS
ENDMETHOD. "constructor
ENDCLASS. "lcl_sub IMPLEMENTATION
START-OF-SELECTION.
DATA: obj1 TYPE REF TO lcl_super,
obj2 TYPE REF TO lcl_sub.
CREATE OBJECT obj1.
CREATE OBJECT obj2.
CALL METHOD obj1->m2. "This will call M2 of the super class
obj1 = obj2.
CALL METHOD obj1->m2. "This will call M2 of the sub class
Hope helpful
Raghunath.S