Tip

Using subclasses to develop applications that run on different releases of SAP

During a recent project, I encountered the following problem: I'd written an end-user report, and naturally, I was using CL_SALV_TABLE. But I then discovered my program was needed in a Basis 6.20 system.

But CL_SALV_TABLE doesn't exist in 6.20! Oops!

So now the question became, how could I rewrite the program so that in Basis 6.40 it would use the newer ABAP classes while using the older ABAP classes in 6.20?

Here's the solution I came up with, using the power of

    Requires Free Membership to View

OOP (object-oriented programming). I created an abstract superclass ZCL_SUPER, which has a static method – FACTORY -- and a public abstract instance method, DISPLAY.

Next I created two subclasses of ZCL_SUPER. One subclass, ZCL_620, implemented DISPLAY using CL_GUI_ALV_GRID. The other subclass, ZCL_640, implemented DISPLAY using CL_SALV_TABLE.

ZCL_SUPER=>FACTORY has a returning parameter, r_ref TYPE REF TO zcl_super.

IF sy-saprl EQ '620'.
 CREATE OBJECT r_ref TYPE zcl_620.
ELSE.
 CREATE OBJECT r_ref TYPE zcl_640.
ENDIF.

My calling program is: DATA: r_output TYPE REF TO zcl_super. " Get data into the internal table it_mydata r_output = zcl_super=>factory( ). r_output->display( it_mydata ).

Due to inheritance, the display method for the correct class will automatically be called. The calling program doesn't have to be concerned about which release of SAP Basis it is on.

Ah, I hear you say, when you import this code into the 6.20 system, ZCL_640 will have syntax errors. This is true.

More on SAP application development
Read Matthew's tip on how to find a piece of ABAP code without going through debugging hell

Read Matthew's answers to reader questions related to SAP application development.

However, subclasses are loosely coupled to the superclass ( the subclass knows who its superclass is, but the superclass doesn't know, or care, who its subclasses are), so it doesn't matter! The program will still work – ZCL_620 and ZCL_SUPER won't have any errors and will run perfectly well.

Now, convincing Quality and Basis that an RC8 on a transport import isn't always an issue – that's the hard part!

About the creator of this tip: Matthew Billingham is SearchSAP.com's application development expert. He has worked for companies such as Eurotunnel and Vodafone, served as development manager for Coats PLc, and as head of application development with Novartis Pharma in Switzerland, managing the development side of a global SAP rollout.

Matthew now runs his own SAP consultancy, providing guidance and designing solutions (particularly for intractable problems) for third parties. His expertise is in program design, troubleshooting and knowing just how SAP works; his clients describe him as "Consultant for everything." He is co-founder of SecureBI, delivering innovative software solutions in the SAP security space.

This was first published in September 2009

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.