Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

what is upcasting and downcasting?

Former Member

Can anyone explain me what is an upcasting and downcasting in ABAP Objects with some simple example.

Thanks...

Rose.

2 REPLIES 2

Former Member
0 Kudos

hi camila,

Go to ABAPDOCU tcode and see example programs in abap objects section, you will find separate programs for upcasting and downcasting .

Up-Cast (Widening Cast)

Variables of the type reference to superclass can also refer to subclass instances at runtime.

If you assign a subclass reference to a superclass reference, this ensures that

all components that can be accessed syntactically after the cast assignment are

actually available in the instance. The subclass always contains at least the same

components as the superclass. After all, the name and the signature of redefined

methods are identical.

The user can therefore address the subclass instance in the same way as the

superclass instance. However, he/she is restricted to using only the inherited

components.

In this example, after the assignment, the methods GET_MAKE, GET_COUNT,

DISPLAY_ATTRIBUTES, SET_ATTRIBUTES and ESTIMATE_FUEL of the

instance LCL_TRUCK can only be accessed using the reference R_VEHICLE.

If there are any restrictions regarding visibility, they are left unchanged. It is not

possible to access the specific components from the class LCL_TRUCK of the

instance (GET_CARGO in the above example) using the reference R_VEHICLE.

The view is thus usually narrowed (or at least unchanged). That is why we

describe this type of assignment of reference variables as up-cast. There is a

switch from a view of several components to a view of a few components. As

the target variable can accept more dynamic types in comparison to the source

variable, this assignment is also called Widening Cast

Static and Dynamic Types of References

A reference variable always has two types at runtime: static and dynamic.

In the example, LCL_VEHICLE is the static type of the variable R_VEHICLE.

Depending on the cast assignment, the dynamic type is either LCL_BUS or

LCL_TRUCK. In the ABAP Debugger, the dynamic type is specified in the form

of the following object display.

Down-cast (Narrowing Cast)

Variables of the type “reference to superclass” can also refer to subclass instances

at runtime. You may now want to copy such a reference (back) to a suitable

variable of the type “reference to subclass”.

If you want to assign a superclass reference to a subclass reference, you must

use the down-cast assignment operator MOVE ... ?TO ... or its short form

?=. Otherwise, you would get a message stating that it is not certain that all

components that can be accessed syntactically after the cast assignment are

actually available in the instance. As a rule, the subclass class contains more

components than the superclass.

After assigning this type of reference (back) to a subclass reference to the

implementing class, clients are no longer limited to inherited components: In the

example given here, all components of the LCL_TRUCK instance can be accessed

(again) after the assignment using the reference R_TRUCK2.

The view is thus usually widened (or at least unchanged). That is why we describe

this type of assignment of reference variables as down-cast. There is a switch

from a view of a few components to a view of more components. As the target

variable can accept less dynamic types after the assignment, this assignment is

also called Narrowing Cast.

reward if helpful

regards,

sravanthi.

Former Member
0 Kudos

hi,

A super class reference can be converted into a subclass reference but this

subclass reference is not useful to call the methods of any of the classes. This convention is known as Downcasting.

We can convert the subclass reference to a superclass reference without using the cast poeration compiler because it will internally take care of the casting. This is known as Upcasting.

Up-casting :

Now we come on to one of the original goals of the article. With the simple example above of polymorphism, you should be able to quickly understand what up-casting is, in fact we have already used up-casting in our example.

Consider the following code:

Shape s = new Circle(100, 100);

We have cast Circle to the type Shape. This is perfectly legal code (as we saw in the Polymorphism example). This is possible, because Circle has been derived from Shape and you expect all methods and properties of Shape to exist in Circle.

So why is this called up-casting? Consider the diagram above. From Circle, we are moving up the object hierarchy to the type Shape, so we are casting our object "upwards" to its parent type.

Up-casting is implicit and is safe. What do we mean by safe? Well, we can happily cast Circle to Shape and expect all the properties and methods of Shape to be available.

Down-casting :

The flip side of the coin to up-casting is ...yes you guessed it, down-casting. Down-casting takes a little more understanding and you have to be very careful when down-casting types.

To help us better understand down-casting, we are going to add a new method to our Circle class. This will be a simple method called FillCircle.

public void FillCircle()

{

Console.WriteLine("Filling CIRCLE at {0},", m_xpos, m_ypos);

}

Using the example from up-casting, we know that we are able to write the following:

Shape s = new Circle(100, 100);

We are then free to call the Draw method. Having added the FillCircle method to our Circle class, are we able to call this method by doing the following?

s.FillCircle ();

In short, the answer is no. Because we have cast Circle to the type Shape, we are only able to use methods found in Shape, that is, Circle has inherited all the properties and methods of Shape. If we want to call FillCircle, we need to down-cast our type to Circle. Why is it called down-casting? Quite simply, we are moving down the object hierarchy, from Shape down to Circle.

So how do we code a down-cast from Shape to Circle? The code for doing this is quite simple:

Circle c;

c = (Circle)s;

Simply, we are declaring c as the type Circle and explicitly casting s to this type. We are now able to call the FillCircle method by doing the following:

c.FillCircle();

This gives us the following output:

Drawing a CIRCLE at 100,100

Filling CIRCLE at 100,100

We could also write ((Circle)s).FillCircle() reducing the lines of code needed to down-cast our type and call the required method.

Down-casting is potentially unsafe, because you could attempt to use a method that the derived class does not actually implement. With this in mind, down-casting is always explicit, that is, we are always specifying the type we are down-casting to.

regards,

vasavi.

reward if helpful.