New Way of Object Casting

light111New Way of Object Casting in ABAP

In OO ABAP, for object down casting operator[  ?=  ] is used. But now we have the new [  CAST  ] operator which can do the same for the object down casting.


CLASS c1 DEFINITION.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.
CLASS c3 DEFINITION INHERITING FROM c1.
ENDCLASS.

START-OF-SELECTION.
DATA: obj1 TYPE REF TO c1,
obj2 TYPE REF TO c2,
obj3 TYPE REF TO c2,
obj4 TYPE REF TO c2.

CREATE OBJECT obj2.
* Up Casting
obj1 = obj2.

* Old Down Casting Syntax
obj3 ?= obj1.

* New Downcasting Syntax
obj4 = CAST #( obj1 ).


Result:

1.jpg


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s