MichaelB.
7/28/2018 - 9:51 AM

Define a DEFINITION section of a class

A class definition can define the following: Types, constants, data objects (attributes), and method interfaces. We can choose how we define the visibility of attributes: Public - available from outside the class. Attributes are visible to the calling program, which can change attribute content of an object declared using this class. It can also call methods that are declared as public methods. Private - available only within the class itself. Calling program cannot access the elements of an object directly. Normally you would create a public method that carries out actions on private elements of an object. Protected - any elements defined here are used for object inheritance. A public attribute may be designated as READ-ONLY. A READ-ONLY attribute only really makes sense in a PUBLIC SECTION. Use it to only let your object method update the variable and not the calling program. You still want it made visible to external coding programs but don't want to allow them to change the variable.Within the class definition, the public section must come before the private section.

CLASS car DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA numofcars  TYPE i.         "Static Attribute

    METHODS setnumseats
      IMPORTING
        newseatnum TYPE i.

    METHODS gofaster
      IMPORTING
        increment TYPE i
      EXPORTING
        result    TYPE i.

    METHODS goslower
      IMPORTING
        increment     TYPE i
      RETURNING
        VALUE(result) TYPE i.

  PRIVATE SECTION.
    DATA: make     TYPE c LENGTH 20,
          model    TYPE c LENGTH 20,
          numseats TYPE i,
          speed    TYPE i,
          maxspeed TYPE i.

ENDCLASS.