What does ‘?=’ means in CL_SALV_TABLE class

This ‘?=’ denotes a widening cast operator. In widening cast we assign reference of superclass to reference of subclass.

Suppose u have a superclass lcl_vehicle

its reference is r_vehicle.

abd you have its subclass lcl_car

with reference r_car.

r_car ?= r_vehicle.

‘?=’ denotes the WIDECASTING Operator in ABAP Object.

The assignment of an object reference to an interface reference is known as a narrowing cast since, as with inheritance, only a part of the object interface is visible once you have assigned the reference.

With an interface reference, you can no longer address all components in the class carrying out the implementation, but only the components defined in the interface. These components are now addressed using the interface reference exclusively with their own u2018shortu2019 name!

When an object reference is assigned to an interface reference, the static types Must be convertible, that is, the class that was used to define the object reference must have implemented the interface-reference interface. Otherwise there will be a syntax error.

The widening cast is, as with inheritance, the opposite of the narrowing cast: here it is used to retrieve an object reference from an interface reference. Obviously it cannot be statically checked, since an interface can be implemented by more than one class.

An object reference cannot be assigned to an interface reference if it has itself not implemented the corresponding interface. It cannot be assigned even if a subclass has implemented the interface and the interface reference points to an object in this class.

Assignments between interface references whose interfaces are not related to each other cannot be checked statically and must therefore be formulated using the cast operator u201C?=u201D.

For this type of assignment, a check must be carried out at runtime to see whether the class of the instance that the source reference points to also supports the interface that the target reference refers to. If this is the case, the cast is carried out, otherwise the catchable runtime MOVE_CAST_ERROR occurs.

This type of cast is neither a widening nor a narrowing cast, rather a switch from one view of an object to another.

Leave a comment