Monday, February 11, 2013

Object Oriented Classic Program for Single Table

Class is the type of object. We can say that the class is a template of object. It is the abstract description of object. The components of the class define the attributes of the object. In ABAP we have two types of classes - Global class and Local class.

Global class is defined in the Class builder (Transaction SE24). We can create and define a global class here. All ABAP programs can access Global class.

Local classes are defined in local program. That program only accesses these classes. When we use class in our program the system searches that class locally at first. If it doesn’t find this one then it looks for global classes.

  • Class contains components / attributes.
  • Each component is assigned to a visibility section (public / protected / private).
  • Class implements methods.

Class Components:
Any variable, constant, structure, work area, table declared inside the class are called class components. Methods are also treated as components of that class. This declaration must be defined under any of its visibility section. Hence according to the visibility the components will be available to the users of the class.

Visibility Section:
It defines the interface between the class and its users. The three visibility sections are as follows.

Public Section – All users can access any components, methods declared in public section. Hence the components can be accessed by the same class or any of its sub classes or all users defined in the same program. Public components form an interface between the class and its users.

Protected Section – Components and methods declared in the protected section can be accessed to all methods of this class and its sub classes which are inherited from it. Protected components form a special interface between the class and its inherited sub classes.

Private Section – Components and methods declared in the private section of a class can only be accessed by that class. Private components don’t form any external interface.

Here is a program which shows a list of data from a single table MARA based on the input on Selection Screen. Selection screen contains a select-option and the pattern is block b1. We are displaying 4 fields of MARA like Material, Date, Name & Type. 

Structure of internal table and the declaration of internal table are in the Public section of the class so that every attributes and methods can have the access of that. In the implementation part we are selecting fields from MARA and writing the desired output.

We are creating instance under the START-OF-SELECTION event and calling the implemented method. Here the object must be reference to the class.

REPORT  zsr_test.

*-------Declaration of Table for Select Option-------------------------*
TABLES: mara.

*----Event Initialization----------------------------------------------*

*-------Selection Screen with B1 block---------------------------------*

  "Mandatory Selection range
  SELECT-OPTIONS  s_matnr FOR mara-matnr OBLIGATORY.
*       Definition of Class

  PUBLIC SECTION. "Public section property

    "Internal table structure
    TYPES: BEGIN OF ty_mara,
            matnr TYPE mara-matnr,
            ersda TYPE mara-ersda,
            ernam TYPE mara-ernam,
            mtart TYPE mara-mtart,
           END OF ty_mara.

    "Declaration of work area & internal table
    DATA: wa_mara TYPE ty_mara,
          it_mara TYPE STANDARD TABLE OF ty_mara.

    "Declaring Methods
    METHODS: met1.

  PROTECTED SECTION"Protected section property
  PRIVATE SECTION.    "Private section property

ENDCLASS.                    "cls1 DEFINITION

*       Class Implementation - method is defined here

  "Implementing the method
  METHOD met1.

    SELECT matnr ersda ernam mtart
      FROM mara INTO TABLE it_mara
      WHERE matnr IN s_matnr.

    IF sy-subrc = 0.
      SORT it_mara BY matnr.
      LOOP AT it_mara INTO wa_mara.
        AT FIRST.
          WRITE: /3 'MATERIAL', 20 'DATE', 33 'NAME', 43 'TYPE'.
        WRITE: / wa_mara-matnr,
      MESSAGE 'No data found' TYPE 'I'.

  ENDMETHOD.                                                "met1

ENDCLASS.                    "cls1 IMPLEMENTATION

*-----Event Start of Selection-----------------------------------------*

  "Declaring object / instance referenced to the class
  DATA: obj1 TYPE REF TO cls1.

  "Creating the object

  "Calling the method by object
  CALL METHOD: obj1->met1.

Ouput of Selection Screen:

By giving the proper value the List output is:
Here Material No has been selected from 601010007 to 601010050. Since 601010050 is not in the database the last Material no is 601010049.


yektek training said...

Very nice information given by you thank you

siva brahma said...

Very Very Helpfull Information.
If Possible Please Include IDOCs, Webdynpro, Workflow,Adobeforms also.

Thanks in Advance

Bohdan said...

Thank you for a helpful article. It helped me in creation of my first class and method.

baaluu4u said...
This comment has been removed by the author.
Jatin Singhal said...

Very nice document .. Thank you for sharing...

Иван Тюменьев said...


Using classes and objects - is not a OO paradigm. You are giving very bad example of procedure-functional code, written in classes.

If you want to use OO programming, you need to see all picture as objects. For example: object data, object list report, etc.

In this concrete example you need to make 2-3 classes like MVC-pattern.

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

Shalini said...

Hi, This is shalini from Chennai learned SAP Training in Chennai from mr.karthick. The training really was good and i got selected in leading mnc company as SAP Consultant.

You can contact 8122241286 for Best SAP Training in Chennai or reach

sanayamohit said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick

Both Classroom/Online Training is Available!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

Unknown said...

Best SAP Success Factors Training Institute in Chennai
Best SAP MM Training in Chennai
Best SAP SD Training in Chennai
Best SAP ABAP Training in Chennai
Best SAP FICO Training in Chennai
Best SAP BASIS Training in Chennai

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick

Both Classroom/Online Training is Available!!!!!!

Keerthi Suresh said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick

Both Classroom/Online Training is Available!!!!!!

ragu varan said...

Best SAP HANA Training in Chennai by leading HANA Consultant.
Reach at 9003085882

calfre services said...

this site has lot of advantages thank you for sharing wonderfull info a bot sap hr abap sap hr abap online classes