Você está na página 1de 7

Understanding Widening Cast in ABAP Objects

There are many discussions about Widening cast in SCN. some say, it is Up casting while other say it is Down
casting. I remember in a way as depicted below and still following the same.

Let's take a simple inheritance tree.


Narrowing Cast:


Assigning/copying the instance of sub class to super
class instance is called Narrowing Cast.
As shown in the figure, assigning sub class to super class
is going up wards. From sub class(es) to super class
while moving Up the path becomes narrow as shown in
the left image. So I simply remember this. Narrowing cast
= Up cast .








Widening Cast:


Assigning/coping the instance of super class to the sub class
instance is called Widening Cast.
This is the reverse of Narrowing cast. As shown in the figure,
assigning super class to sub class is going down wards.
From super class to sub class while moving Down the path
becomes wider as shown in the left image. So I simply
remember this - Widening cast = Down cast.








I will still remember the above inheritance tree while referring casting and refer Widening cast as Down casting
( though is is referred as up casting from release 7.0).

Coming to the topic, I use Widening cast in this blog (instead of up/down casting ) .

To understand the Widening cast, I will just explain with the simple program.

Simple Inheritance Report
REPORT zkk_widening_cast_demo.
Simple Inheritance Report

CLASS lcl_parent DEFINITION.
PUBLIC SECTION.
METHODS parent_method.

ENDCLASS. "lcl_parent DEFINITION

CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.
PUBLIC SECTION.
METHODS parent_method REDEFINITION.
METHODS child_method.

ENDCLASS. "lcl_child DEFINITION

CLASS lcl_parent IMPLEMENTATION.

METHOD parent_method.
WRITE / 'Called -> parent method in Parent Class!'.
ENDMETHOD. "parent_method
ENDCLASS. "lcl_parent IMPLEMENTATION


CLASS lcl_child IMPLEMENTATION.

METHOD parent_method.
WRITE / 'Called -> parent redifinition method in Child Class!'.
ENDMETHOD. "parent_method
METHOD child_method.
WRITE / 'Called -> child method in Child Class!'.
ENDMETHOD. "child_method

ENDCLASS. "lcl_child IMPLEMENTATION

START-OF-SELECTION.

DATA: lr_parent TYPE REF TO lcl_parent,
lr_child TYPE REF TO lcl_child.

CREATE OBJECT: lr_parent,lr_child.

Simple Inheritance Report
lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).

Now after executing this I got the below output:


Now I am interested in child(sub) class method in my parent(super) class. Which mean when I call the
'parent_method' of parent class in the above report then it should call the 'parent_method' of child class(
Redefined method call instead of original method call). So I do a Narrowing cast.

continuing the above report.

1. CREATE OBJECT: lr_parent,lr_child.
2.
3. WRITE / 'Before Narrowing Cast:'.
4. lr_parent->parent_method( ).
5. lr_child->parent_method( ).
6. lr_child->child_method( ).
7.
8. * Narrowing cast
9. lr_parent = lr_child.
10.
11. WRITE / 'After Narrowing Cast:'.
12. lr_parent->parent_method( ).
13. lr_child->parent_method( ).
14. lr_child->child_method( ).

Now when I execute the above report, I get the below output, which is as expected.


Now I am interested in parent(super) class method in my child(sub) class. Which mean when I call the
'parent_method' of child class in the above report then it should call the 'parent_method' of super class( Super
class method call instead of redefined sub class method call). So I do a Widening cast.

1. CREATE OBJECT: lr_parent,lr_child.
2.
3. WRITE / 'Before Widening Cast:'.
4. lr_parent->parent_method( ).
5. lr_child->parent_method( ).
6. lr_child->child_method( ).
7.
8. TRY .
9. * Widening Cast
10. lr_child ?= lr_parent.
11.
12. WRITE / 'After Widening Cast:'.
13. lr_parent->parent_method( ).
14. lr_child->parent_method( ).
15. lr_child->child_method( ).
16. CATCH cx_sy_move_cast_error.
17. WRITE / 'Widening Cast Failed!'.
18. ENDTRY.

The output is as below:

After seeing the above output, we remember that " It is always not possible to do Widening cast as the sub
class will have more functionality compared to super class" .

So, I removed the method definition 'child_method' in the child class and tried Widening cast. I got the same
output!

I created an empty class definitions and tried widening cast to see the widening cast success message .

CLASS lcl_parent DEFINITION.

ENDCLASS. "lcl_parent DEFINITION


CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.

ENDCLASS. "lcl_child DEFINITION


CLASS lcl_parent IMPLEMENTATION.

ENDCLASS. "lcl_parent IMPLEMENTATION


CLASS lcl_child IMPLEMENTATION.

ENDCLASS. "lcl_child IMPLEMENTATION


START-OF-SELECTION.

DATA: lr_parent TYPE REF TO lcl_parent,
lr_child TYPE REF TO lcl_child.

CREATE OBJECT: lr_parent,lr_child.

TRY .
* Widening Cast
lr_child ?= lr_parent.
WRITE / 'Widening Cast Success!'.
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.

Now we will see the output which we are all waiting for!

1. Wider/up casting :
This means a ref object used is of a parent. Now here technically speaking you can only call the
methods of the parent class and not of the child class. Even if the parent object is referencing to a child object
at runtime, the compiler does not allow you to call the child method.
So there is an error as it can be detected by the compiler and therefore does not have run-time errors.

2. Narrow/down casting :
This means a ref object used is of a child. Now here technically speaking you can call all the methods
of the parent class as well as of the child class. So in this case, at runtime if the child object point to a prent
object then it will cause an issue if a method specific to a child is called.For the compiler it is not possible to
detect this at compile time, like in the above case and therefore a runtime exception is possible.

Você também pode gostar