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

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

No comments