Friday, March 31, 2017

Guess the output : OO ABAP CASTING Sample Code



Casting Example


*&---------------------------------------------------------------------*
*& Report YTEST_OOABAP5
*&
*&---------------------------------------------------------------------*
*& Upcast/Widening Cast
*& Dowbcast/Narrow cast
*&---------------------------------------------------------------------*

REPORT ytest_ooabap5.

*----------------------------------------------------------------------*
* CLASS property DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property DEFINITION.
PUBLIC SECTION.
METHODS : get_property IMPORTING business_partner TYPE char10 RETURNING value(address) TYPE string ,
set_property IMPORTING business_partner TYPE string address TYPE string,
constructor IMPORTING business_partner TYPE string address TYPE string,
display .
PROTECTED SECTION.
DATA : bp TYPE char10, addr TYPE string.
ENDCLASS. "property DEFINITION

*----------------------------------------------------------------------*
* CLASS property_transfer DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property_transfer DEFINITION INHERITING FROM property.
PUBLIC SECTION.
METHODS : change_property_owner CHANGING value(business_partner) TYPE char10,
display REDEFINITION.
ENDCLASS. "property_transfer DEFINITION

*----------------------------------------------------------------------*
* CLASS property IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property IMPLEMENTATION.
METHOD constructor.
bp = business_partner.
addr = address.
ENDMETHOD. "constructor
METHOD get_property.
address = addr.
ENDMETHOD. "get_property
METHOD set_property.
addr = address.
ENDMETHOD. "set_property
METHOD display.
ULINE.
WRITE : 'Parent'.
ULINE.
ENDMETHOD. "display
ENDCLASS. "property IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS property_transfer IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property_transfer IMPLEMENTATION.
METHOD change_property_owner.
bp = business_partner.
ENDMETHOD. "change_property_owner
METHOD display.
ULINE.
WRITE : 'Child'.
ULINE.
ENDMETHOD. "display
ENDCLASS. "property_transfer IMPLEMENTATION

START-OF-SELECTION.
DATA : lo_property TYPE REF TO property,
lo_property_transfer TYPE REF TO property_transfer.

DATA : lv_bp TYPE char10 VALUE '153',
lv_error TYPE REF TO cx_root.


CREATE OBJECT lo_property EXPORTING business_partner = '151' address = 'HSR' .
CREATE OBJECT lo_property_transfer EXPORTING business_partner = '152' address = 'BTM' .

* lo_property = lo_property_transfer.
* lo_property->change_property_owner( business_partner = '153' ). " Syntax error : Object does not exist

* lo_property_transfer = lo_property. "Syntax Error : Type conversion
lo_property = lo_property_transfer.

TRY.
lo_property_transfer ?= lo_property.
lo_property_transfer->change_property_owner( CHANGING business_partner = lv_bp )."'CX_SY_MOVE_CAST_ERROR'
CATCH cx_sy_move_cast_error INTO lv_error.
WRITE : 'CAST ERROR'.
ENDTRY.

lo_property->display( ).
lo_property_transfer->display( ).





OUTPUT



Test Program


Child

Child

No comments:

Post a Comment