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----------------------------------------------*
INITIALIZATION.

*-------Selection Screen with B1 block---------------------------------*
  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

  "Mandatory Selection range
  SELECT-OPTIONS  s_matnr FOR mara-matnr OBLIGATORY.
  SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*       Definition of Class
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.

  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 cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*       Class Implementation - method is defined here
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.

  "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'.
          ULINE.
          SKIP.
        ENDAT.
        WRITE: / wa_mara-matnr,
                 wa_mara-ersda,
                 wa_mara-ernam,
                 wa_mara-mtart.
      ENDLOOP.
    ELSE.
      MESSAGE 'No data found' TYPE 'I'.
    ENDIF.

  ENDMETHOD.                                                "met1

ENDCLASS.                    "cls1 IMPLEMENTATION

*-----Event Start of Selection-----------------------------------------*
START-OF-SELECTION.

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

  "Creating the object
  CREATE OBJECT: obj1.

  "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.


9 comments:

Unknown said...

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

Thanks in Advance
sivabrahma.k@gmail.com

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...

Unknown said...

Hello.

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.

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 www.thecreatingexperts.com

Anonymous 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..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

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

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

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

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
http://thecreatingexperts.com/sap-mm-training-in-chennai/
http://thecreatingexperts.com/sap-sd-training-in-chennai/
http://thecreatingexperts.com/sap-hr-training-in-chennai/
http://thecreatingexperts.com/sap-fico-training-in-chennai/
http://thecreatingexperts.com/sap-abap-training-in-chennai/
http://thecreatingexperts.com/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
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

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

Unknown said...

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