banner ads

Object Oriented Classic Program for Single Table

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


No comments