14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2

New Language Features in ABAP 7.4

ABAP Test Cockpit

Speaking of FOR ALL ENTRIES, any programmer with even a little experience will have

fallen into the trap in which FOR ALL ENTRIES can bring down your program when you

pass in an empty table. Happily, when Chap ter 4 looks at the ABAP Test Cockpit, you

will see that the syntax chec k now warns you to test that LT_MONSTERS is not empty

when doing such a thing to avoid the bug in SAP whereby an empty table retrieves the

whole database table.

2.7 Object-Oriented Programming

SAP introduced the concept of object-oriented programming in ABAP in the year

2000. Since then, it has been the recommended method of programming. This

section will describe the new features of ABAP 7.4 that relate to object-oriented

programming.

2.7.1 Upcasting/Downcasting with CAST

In OO programming, a downcast is a process in which you turn a generic object,

like a monster, into a more specific object, like a green monster. An upcast is the

reverse. This functionality has been available in ABAP for a long time, but it gets

a lot easier in 7.4.

To take a non-monster-related example for once, consider a situation where you

need to get all the components of a spec ific dictionary structure. Listing 2.69

shows how you would do this prior to ABAP 7.4. First of all, you call a method of

CL_ABAP_TYPEDESCR to get metadata about a certain structure. However, to get the

list of components of that structure into an internal table, you need an instance of

CL_ABAP_STRUCTDESCR; this is a subclass of CL_ABAP_TYPEDESCR. Thus, you need to

do a downcast to convert the instance of the parent class into an instance of the

subclass.

DATA lo_structdescr TYPE REF TO cl_abap_structdescr.

lo_structdescr ?= cl_abap_typedescr=>describe_by_name( 'ZSC_MONSTER_

HEADER' ).

DATA lt_components TYPE abap_compdescr_tab.

lt_components = lo_structdescr->components.

Listing 2.69 Components of a Specific Dictionary Structure without CAST

126

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!