0% found this document useful (0 votes)
153 views

Casting in ABAP Objects

Casting in ABAP Objects involves narrowing casts (upcasting) and widening casts (downcasting). Narrowing casts assign a subclass instance to a supertype reference, viewing the object less specifically. Widening casts assign a supertype instance to a subclass reference, viewing the object more specifically. The example demonstrates narrowing by assigning a circle object to a shape reference, and widening by conditionally assigning a shape reference to a circle reference using a downcast.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views

Casting in ABAP Objects

Casting in ABAP Objects involves narrowing casts (upcasting) and widening casts (downcasting). Narrowing casts assign a subclass instance to a supertype reference, viewing the object less specifically. Widening casts assign a supertype instance to a subclass reference, viewing the object more specifically. The example demonstrates narrowing by assigning a circle object to a shape reference, and widening by conditionally assigning a shape reference to a circle reference using a downcast.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Casting In ABAP Objects

Type of Casting:
1. Narrowing Cast( Upcasting):- When we assign the instance of the Sub class back to the instance
of the Super class, than it is called the "Narrow Casting", because we are switching from
a "More Specific view of an object" to "less specific view".
2. Widening Cast(Downcasting):- When we assign the instance of the Super class to the Subclass,
than it is called the Widening Cast, because we are moving to the "More Specific View"
from the "Less specific view".

CLASS lcl_shape DEFINITION.


PUBLIC SECTION.
METHODs draw.
ENDCLASS.
CLASS lcl_circle DEFINITION INHERITING FROM lcl_shape.
PUBLIC SECTION.
METHODS: draw REDEFINITION,
calc_area.
ENDCLASS.
CLASS lcl_shape IMPLEMENTATION.
METHOD draw.
WRITE :/ 'Drawing any Shape'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_circle IMPLEMENTATION.
METHOD draw.
WRITE :/ 'Drawing specific shape: Circle'.

ENDMETHOD.
METHOD calc_area.
WRITE :/ 'Area Of Crcle = 2iiR'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
data : o_cir type REF TO lcl_circle.
create OBJECT o_cir.
call METHOD o_cir->draw( ). " calls subclass Draw() method
call METHOD o_cir->calc_area( ).
uline.
"--------- Narrow cast(Upcast)---------------"
data : o_shp type REF TO lcl_shape.
o_shp = o_cir. " Narrow cast(Upcast)
call METHOD o_shp->draw( ). " calls sub class Draw() method
"call METHOD o_shp->calc_area( ) . " compilation error
uline.
"---------- Widening Cast(Downcast) -----------"
data : o_cir1 type REF TO lcl_circle.
" o_cir1 = o_shp. " complilation erroro_cir1 ?= o_shp. " Widening Cast(Downcast)
call METHOD o_cir1->draw( ). " calls subclass Draw() method
call METHOD o_cir1->calc_area( ).
-------------------------------------------------------Output-------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy