6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24OOP2808 – Classes and Objects in OO Programming

What is a Class?

A class in SAP ABAP is like a blueprint used to create objects. It holds data (attributes) and the methods (functions) that operate on this data. Think of a class as a template that defines the structure and behavior for the objects you create from it.

Example:

CLASS zcl_vehicle DEFINITION.

  PUBLIC SECTION.

    METHODS: start_engine

             stop_engine.

    DATA: model TYPE string

          color TYPE string.

ENDCLASS.

 

CLASS zcl_vehicle IMPLEMENTATION.

  METHOD start_engine.

    WRITE: ‘Engine started.’.

  ENDMETHOD.

 

  METHOD stop_engine.

    WRITE: ‘Engine stopped.’.

  ENDMETHOD.

ENDCLASS.

 

In this example, zcl_vehicle is a class with two methods (start_engine and stop_engine) and two attributes (model and color).

 

What is an Object?

An object is an instance of a class. When you create an object, it uses the class as its template. Each object has its own data and can perform the methods defined in the class.

For example:

DATA: vehicle1 TYPE REF TO zcl_vehicle

      vehicle2 TYPE REF TO zcl_vehicle.

 

CREATE OBJECT vehicle1.

CREATE OBJECT vehicle2.

 

vehicle1->model = ‘Car’.

vehicle1->color = ‘Red’.

 

vehicle2->model = ‘Bike’.

vehicle2->color = ‘Blue’.

 

vehicle1->start_engine().

vehicle2->start_engine().

 

Here, vehicle1 and vehicle2 are objects of the class zcl_vehicle. They each have their own set of attributes (model and color) and can act independently.

 

Components of a Class in SAP ABAP

A class in SAP ABAP consists of several key components:

  1. Attributes (Data Components):

   – Attributes store the data within an object and define its state.

   – Instance Attributes: Specific to each object instance.

   – Static Attributes: Shared across all instances of a class.

Example:

   DATA: model TYPE string

         color TYPE string.

 

  1. Methods (Behavior Components):

   – Methods define the actions an object can perform. They contain the business logic.

   – Instance Methods: Operate on specific object instances.

   – Static Methods: Can be called directly using the class name without needing an object.

   Example:

  METHODS: start_engine

            stop_engine.

 

  1. Events:

   – Events are custom notifications that other objects can respond to.

   – Declared using the EVENTS statement and raised within a method.

   Example:

EVENTS: engine_started.

 

  1. Interfaces:

   – Interfaces define a set of methods without implementing them. A class that implements an interface must provide the method implementations.

   – Defined using the INTERFACE keyword.

   Example:

   INTERFACE if_vehicle.

     METHODS: start_engine

              stop_engine.

   ENDINTERFACE.

 

  1. Visibility Sections:

   – Visibility determines how and where the class’s components can be accessed.

   – Public: Accessible by all objects.

   – Protected: Accessible by the class itself and its subclasses.

   – Private: Accessible only within the class itself.

   Example:

   CLASS zcl_vehicle DEFINITION.

     PUBLIC SECTION.

       METHODS: start_engine

                stop_engine.

       DATA: model TYPE string.

     PRIVATE SECTION.

       DATA: color TYPE string.

   ENDCLASS.

 

  1. Constructor and Destructor:

   – The Constructor is automatically called when an object is created and initializes attributes.

   – The Destructor is called when an object is destroyed and cleans up resources.

Example:

   CLASS zcl_vehicle DEFINITION.

     PUBLIC SECTION.

       METHODS: constructor

                destructor.

   ENDCLASS.

 

   CLASS zcl_vehicle IMPLEMENTATION.

     METHOD constructor.

       WRITE: ‘Object created.’.

     ENDMETHOD.

 

     METHOD destructor.

       WRITE: ‘Object destroyed.’.

     ENDMETHOD.

   ENDCLASS.

 

Author : Aniket Pawar, 9373518385                                                     

24OOP2808 – Classes and Objects in OO Programming

What is a Class?

A class in SAP ABAP is like a blueprint used to create objects. It holds data (attributes) and the methods (functions) that operate on this data. Think of a class as a template that defines the structure and behavior for the objects you create from it.

Example:

CLASS zcl_vehicle DEFINITION.

  PUBLIC SECTION.

    METHODS: start_engine

             stop_engine.

    DATA: model TYPE string

          color TYPE string.

ENDCLASS.

 

CLASS zcl_vehicle IMPLEMENTATION.

  METHOD start_engine.

    WRITE: ‘Engine started.’.

  ENDMETHOD.

 

  METHOD stop_engine.

    WRITE: ‘Engine stopped.’.

  ENDMETHOD.

ENDCLASS.

 

In this example, zcl_vehicle is a class with two methods (start_engine and stop_engine) and two attributes (model and color).

 

What is an Object?

An object is an instance of a class. When you create an object, it uses the class as its template. Each object has its own data and can perform the methods defined in the class.

For example:

DATA: vehicle1 TYPE REF TO zcl_vehicle

      vehicle2 TYPE REF TO zcl_vehicle.

 

CREATE OBJECT vehicle1.

CREATE OBJECT vehicle2.

 

vehicle1->model = ‘Car’.

vehicle1->color = ‘Red’.

 

vehicle2->model = ‘Bike’.

vehicle2->color = ‘Blue’.

 

vehicle1->start_engine().

vehicle2->start_engine().

 

Here, vehicle1 and vehicle2 are objects of the class zcl_vehicle. They each have their own set of attributes (model and color) and can act independently.

 

Components of a Class in SAP ABAP

A class in SAP ABAP consists of several key components:

  1. Attributes (Data Components):

   – Attributes store the data within an object and define its state.

   – Instance Attributes: Specific to each object instance.

   – Static Attributes: Shared across all instances of a class.

Example:

   DATA: model TYPE string

         color TYPE string.

 

  1. Methods (Behavior Components):

   – Methods define the actions an object can perform. They contain the business logic.

   – Instance Methods: Operate on specific object instances.

   – Static Methods: Can be called directly using the class name without needing an object.

   Example:

  METHODS: start_engine

            stop_engine.

 

  1. Events:

   – Events are custom notifications that other objects can respond to.

   – Declared using the EVENTS statement and raised within a method.

   Example:

EVENTS: engine_started.

 

  1. Interfaces:

   – Interfaces define a set of methods without implementing them. A class that implements an interface must provide the method implementations.

   – Defined using the INTERFACE keyword.

   Example:

   INTERFACE if_vehicle.

     METHODS: start_engine

              stop_engine.

   ENDINTERFACE.

 

  1. Visibility Sections:

   – Visibility determines how and where the class’s components can be accessed.

   – Public: Accessible by all objects.

   – Protected: Accessible by the class itself and its subclasses.

   – Private: Accessible only within the class itself.

   Example:

   CLASS zcl_vehicle DEFINITION.

     PUBLIC SECTION.

       METHODS: start_engine

                stop_engine.

       DATA: model TYPE string.

     PRIVATE SECTION.

       DATA: color TYPE string.

   ENDCLASS.

 

  1. Constructor and Destructor:

   – The Constructor is automatically called when an object is created and initializes attributes.

   – The Destructor is called when an object is destroyed and cleans up resources.

Example:

   CLASS zcl_vehicle DEFINITION.

     PUBLIC SECTION.

       METHODS: constructor

                destructor.

   ENDCLASS.

 

   CLASS zcl_vehicle IMPLEMENTATION.

     METHOD constructor.

       WRITE: ‘Object created.’.

     ENDMETHOD.

 

     METHOD destructor.

       WRITE: ‘Object destroyed.’.

     ENDMETHOD.

   ENDCLASS.

 

Author : Aniket Pawar, 9373518385