SAP OOABAP Concept Wide Casting or Up Casting
Definition: - The assignment of a super class
instance to a reference variable of the type "points to sub class" is
described as a Wide casting, because here we are switching from a more generalized
view to a one with more detail (Specialized View).
Note:- In order to implement wide casting
first we need to implement the narrow casting.
Steps:-
Here I am taking the example of calculation of area
of rectangle and area of cube.
a) Definition and implementation of Super Class:-
CLASS class_super DEFINITION.
PUBLIC SECTION.
DATA:
w_area TYPE i.
METHODS:
area IMPORTING w_length TYPE i
PUBLIC SECTION.
DATA:
w_area TYPE i.
METHODS:
area IMPORTING w_length TYPE i
w_breadth TYPE i.
ENDCLASS. "class_super DEFINITION
CLASS class_super IMPLEMENTATION.
METHOD area.
w_area = w_length * w_breadth.
WRITE:/ 'Area of rectangle = ',
ENDCLASS. "class_super DEFINITION
CLASS class_super IMPLEMENTATION.
METHOD area.
w_area = w_length * w_breadth.
WRITE:/ 'Area of rectangle = ',
w_area.
ENDMETHOD. "area
ENDCLASS.
ENDMETHOD. "area
ENDCLASS.
In the above picture the class class_super is the super class and has one method area which is to find the area of
rectangle. The method has two parameters w_length, w_breadth for length and
breadth.
b) Definition and implementation of sub class:-
CLASS class_sub DEFINITION INHERITING FROM class_super.
PUBLIC SECTION.
DATA:
w_height TYPE i.
METHODS:
area REDEFINITION.
ENDCLASS. "class_sub DEFINITION
CLASS class_sub IMPLEMENTATION.
METHOD area.
w_area = 6 * w_length * w_breadth * w_height.
WRITE:/ 'Area of the cube ='.
WRITE: w_area.
ENDMETHOD. "area
ENDCLASS. "class_sub IMPLEMENTATION
PUBLIC SECTION.
DATA:
w_height TYPE i.
METHODS:
area REDEFINITION.
ENDCLASS. "class_sub DEFINITION
CLASS class_sub IMPLEMENTATION.
METHOD area.
w_area = 6 * w_length * w_breadth * w_height.
WRITE:/ 'Area of the cube ='.
WRITE: w_area.
ENDMETHOD. "area
ENDCLASS. "class_sub IMPLEMENTATION
In the above picture the class class_sub is the sub class which is inheriting from the super class
class_super. This sub class has one
method area which is same as that of
super class and redefined for calculating the area of cube. This method has one
more parameter W_height along with w_length and w_breadth. This method finds the area of cube.
c) Declaration of reference variables:-
DATA:
object_superclass TYPE REF TO class_super,
object_subclass TYPE REF TO class_sub,
object2_superclass TYPE REF TO class_super.
object_superclass TYPE REF TO class_super,
object_subclass TYPE REF TO class_sub,
object2_superclass TYPE REF TO class_super.
In above screen two reference variables have been declared for
super class and one reference variable is declared for sub class.
d) Creation of objects:-
START-OF-SELECTION.
CREATE OBJECT object_superclass.
CREATE OBJECT object2_superclass.
CREATE OBJECT object_subclass.
CREATE OBJECT object_superclass.
CREATE OBJECT object2_superclass.
CREATE OBJECT object_subclass.
Now create the objects
for the super and base class. Call the method Area of super class as below:
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 5.
EXPORTING
w_length = 10
w_breadth = 5.
e) Implementation of Narrow casting:- As I discussed in introduction in order to implement wide casting we need to first implement narrow casting.
"Narrow casting
object_subclass->w_height = 10.
object_superclass = object_subclass.
object_subclass->w_height = 10.
object_superclass = object_subclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 10.
EXPORTING
w_length = 10
w_breadth = 10.
We
are assigning the value ‘10’ to ‘w_height’ instance variable of subclass
object.
In
above picture we are assigning the sub class instance reference variable to
super class instance reference variable. This is known as narrow casting.
Now
the instance of the super class(object_superclass) now points or refers to the
object of the subclass, thus the modified method 'area' of the
subclass is accessible. Now we can call the method Area of subclass class_sub to calculate the area of cube.
"Wide casting
object_superclass ?= object2_superclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 5.
The
object 'object_superclass' now refers to the object of the
subclass 'object_subclass'. Thus the 'area'
method of the superclass cannot be called by this object. In order to enable it
to call the 'area' method of the superclass, wide casting has
to be performed.
"Wide casting
object_superclass ?= object2_superclass.
object_superclass ?= object2_superclass.
So
right hand side of wide casting always has object of super class. Left side of
wide casting always is object reference declared as ‘type ref to’ to super
class. Now method area of super class is called again to find the area of rectangle.
Note:- Wide Casting cannot be performed on sub
class objects. It can only be performed on super class objects that have narrow
casting.
Summary:- The assignment of a super class instance to a reference
variable of the type "points to sub class" is described as a Wide
casting, because here we are switching from a more generalized view to a one
with more detail(Specialized View).
Source Code:-
REPORT yh_wide NO STANDARD PAGE HEADING.
CLASS class_super DEFINITION.
PUBLIC SECTION.
DATA:
w_area TYPE i.
METHODS:
area IMPORTING w_length TYPE i w_breadth TYPE i.
ENDCLASS. "class_super DEFINITION
CLASS class_super IMPLEMENTATION.
METHOD area.
w_area = w_length * w_breadth.
WRITE:/ 'Area of rectangle = '.
WRITE: w_area.
ENDMETHOD. "area
ENDCLASS. "class_super IMPLEMENTATION
CLASS class_sub DEFINITION INHERITING FROM class_super.
PUBLIC SECTION.
DATA:
w_height TYPE i.
METHODS:
area REDEFINITION.
ENDCLASS. "class_sub DEFINITION
CLASS class_sub IMPLEMENTATION.
METHOD area.
w_area = 6 * w_length * w_breadth * w_height.
WRITE:/ 'Area of the cube =',
w_area.
ENDMETHOD. "area
ENDCLASS. "class_sub IMPLEMENTATION
DATA:
object_superclass TYPE REF TO class_super,
object_subclass TYPE REF TO class_sub,
object2_superclass TYPE REF TO class_super.
START-OF-SELECTION.
CREATE OBJECT object_superclass.
CREATE OBJECT object2_superclass.
CREATE OBJECT object_subclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 5.
"Narrow casting
object_subclass->w_height = 10.
object_superclass = object_subclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 10.
"Wide casting
object_superclass ?= object2_superclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 5.
CLASS class_super DEFINITION.
PUBLIC SECTION.
DATA:
w_area TYPE i.
METHODS:
area IMPORTING w_length TYPE i w_breadth TYPE i.
ENDCLASS. "class_super DEFINITION
CLASS class_super IMPLEMENTATION.
METHOD area.
w_area = w_length * w_breadth.
WRITE:/ 'Area of rectangle = '.
WRITE: w_area.
ENDMETHOD. "area
ENDCLASS. "class_super IMPLEMENTATION
CLASS class_sub DEFINITION INHERITING FROM class_super.
PUBLIC SECTION.
DATA:
w_height TYPE i.
METHODS:
area REDEFINITION.
ENDCLASS. "class_sub DEFINITION
CLASS class_sub IMPLEMENTATION.
METHOD area.
w_area = 6 * w_length * w_breadth * w_height.
WRITE:/ 'Area of the cube =',
w_area.
ENDMETHOD. "area
ENDCLASS. "class_sub IMPLEMENTATION
DATA:
object_superclass TYPE REF TO class_super,
object_subclass TYPE REF TO class_sub,
object2_superclass TYPE REF TO class_super.
START-OF-SELECTION.
CREATE OBJECT object_superclass.
CREATE OBJECT object2_superclass.
CREATE OBJECT object_subclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 5.
"Narrow casting
object_subclass->w_height = 10.
object_superclass = object_subclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 10.
"Wide casting
object_superclass ?= object2_superclass.
CALL METHOD object_superclass->area
EXPORTING
w_length = 10
w_breadth = 5.
Output:-
No comments:
Post a Comment