Tutorial on Wide casting

...Previous

a)     Implementation of Wide casting:- From below screen shot we implement the wide casting.  

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.

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.

1.     Summary:-  The assignment of a superclass 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).  

2.     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 = 
* w_length * w_breadth * w_height.
    WRITE:/ 
'Area of the cube ='.
    WRITE: 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:-

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image