## Tutorial on Wide casting

By Vineesh Batchu

1.     Introduction: - This tutorial is of OOP’s ABAP on concept of Wide casting.

Definition: - 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).

Note:- In order to implement wide casting first we need to implement the narrow casting.

2.      Steps:-

Here I am taking the example of calculation of area of rectangle and area of cube.

a)     Definition and implementation of Super Class:- 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:- 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:- 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:- Now create the objects for the super and base class.

Call the method Area of super class as below: e)     Implementation of Narrow casting:- As I discussed in introduction in order to implement wide casting we need to first implement narrow casting. 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.  