Mentor SAP

Upcasts

If you assign a subclass reference to a superclass reference, this subclass ensures that all the components that you want to access syntactically after the cast assignment are actually available in the instance. The subclass always contains at least the same components as the superclass always the name and the signature of redefined methods are identical.

 

The user can address only those methods and attributes from the subclass instance that they can from the superclass instance.

 

Note that with redefined methods, the implementation of the subclass is executed using the superclass static type of reference.

 

In this example, after the assignment, you can only access the methods GET_MAKE, GET_COUNT, DISPLAY_ATTRIBUTES, SET_ATTRIBUTES, and ESTIMATE_FUEL of the instance LCL_TRUCK by using the reference GO_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 example using the reference GO_VEHICLE. The view or possible access to methods is either narrowed or left unchanged. 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 Type

Types of a reference variable at runtime in object-oriented programming:

 

In the example, LCL_VEHICLE is the static type of the variable GO_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 following form:

 

object_id<\PROGRAM=program_name\CLASS=dynamic_type>

 

Note: Assignments between reference variables are possible whenever the static type of the target variables is more general or equal to the dynamic type of the source variables.