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

24OOP0209 – Types of Component of a Class

  • The different types of components that make up a class in OO ABAP:
  1. Attributes

Attributes are the variables within a class that hold data. They define the properties of the objects created from the class. Attributes can be public, protected, or private, determining their accessibility within and outside the class.

  Types of Attributes:

  1. Instance Attributes: Specific to an instance of a class (i.e. each object can have different values for these attributes).
  2. Static Attributes:  Shared across all instances of a class (i.e. the value is the same for all objects).

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       DATA: iv_instance_attr TYPE i,

             CLASS-DATA: cv_static_attr TYPE i.

   ENDCLASS.

 

Here, `iv_instance_attr` is an instance attribute and `cv_static_attr` is a static attribute.

 

  1. Methods

   Methods are functions or procedures within a class that define the behavior of the class. They operate on the attributes of the class and can be called to perform actions.

Types of Methods:

  Instance Methods: Operate on instance attributes and require an instance of the class to be called.

   Static Methods: Can be called without an instance of the class and usually operate on static attributes.

Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       METHODS: add_numbers

         IMPORTING

           iv_num1 TYPE i

           iv_num2 TYPE i

         RETURNING

           VALUE(rv_sum) TYPE i.

   ENDCLASS.

 

   The method `add_numbers` is an instance method that adds two numbers.

 

  1. Events

Events are a way to implement the observer pattern in OO ABAP, allowing a class to notify other classes when something of interest happens. A class can trigger an event and other classes can respond to it by defining event handlers.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       EVENTS: data_changed.

   ENDCLASS.

 

   Here, `data_changed` is an event that can be triggered when certain data changes.

 

  1. Event Handlers

Event handlers are special methods designed to respond to events. When an event is triggered, the corresponding event handler is executed.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       METHODS: on_data_changed FOR EVENT data_changed OF lcl_example.

   ENDCLASS.

 

   In this example, `on_data_changed` is an event handler that responds to the `data_changed` event.

 

  1. Constructors

   Constructors are special methods that are automatically called when an object of a class is created. They are used to initialize attributes or perform setup tasks.

   Types of Constructors:

  1.   Instance Constructor: Initializes instance attributes.
  2.  Static Constructor:  Initializes static attributes and is called once before any instance of the class is created.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       METHODS: constructor.

   ENDCLASS.

 

   CLASS lcl_example IMPLEMENTATION.

     METHOD constructor.

       ” Initialization code here

     ENDMETHOD.

   ENDCLASS.

 

   The `constructor` method is the instance constructor used to initialize attributes when an object is created.

 

  1. Interfaces

Interfaces are not a component of a class per se, but they are worth mentioning as they define a contract that a class can implement. An interface specifies a set of methods that the class must implement, promoting code consistency and reusability.

Example:

   INTERFACE lif_example.

     METHODS: add_numbers

       IMPORTING

         iv_num1 TYPE i

         iv_num2 TYPE i

       RETURNING

         VALUE(rv_sum) TYPE i.

   ENDINTERFACE.

 

   A class that implements this interface must define the `add_numbers` method.

 

  1. Aliases

Aliases are used to rename methods inherited from an interface or superclass. This can help resolve naming conflicts or simply make method names more intuitive within the context of the class.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       INTERFACES: lif_example.

       ALIASES: calculate_sum FOR lif_example~add_numbers.

   ENDCLASS.

 

   Here, `calculate_sum` is an alias for the `add_numbers` method inherited from the `lif_example` interface.

 

Author : Aniket Pawar, 9373518385                                                     

24OOP0209 – Types of Component of a Class

  • The different types of components that make up a class in OO ABAP:
  1. Attributes

Attributes are the variables within a class that hold data. They define the properties of the objects created from the class. Attributes can be public, protected, or private, determining their accessibility within and outside the class.

  Types of Attributes:

  1.   Instance Attributes: Specific to an instance of a class (i.e. each object can have different values for these attributes).
  2. Static Attributes:  Shared across all instances of a class (i.e. the value is the same for all objects).

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       DATA: iv_instance_attr TYPE i,

             CLASS-DATA: cv_static_attr TYPE i.

   ENDCLASS.

 

Here, `iv_instance_attr` is an instance attribute and `cv_static_attr` is a static attribute.

 

  1. Methods

   Methods are functions or procedures within a class that define the behavior of the class. They operate on the attributes of the class and can be called to perform actions.

Types of Methods:

  Instance Methods: Operate on instance attributes and require an instance of the class to be called.

   Static Methods: Can be called without an instance of the class and usually operate on static attributes.

Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       METHODS: add_numbers

         IMPORTING

           iv_num1 TYPE i

           iv_num2 TYPE i

         RETURNING

           VALUE(rv_sum) TYPE i.

   ENDCLASS.

 

   The method `add_numbers` is an instance method that adds two numbers.

 

  1. Events

Events are a way to implement the observer pattern in OO ABAP, allowing a class to notify other classes when something of interest happens. A class can trigger an event and other classes can respond to it by defining event handlers.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       EVENTS: data_changed.

   ENDCLASS.

 

   Here, `data_changed` is an event that can be triggered when certain data changes.

 

  1. Event Handlers

Event handlers are special methods designed to respond to events. When an event is triggered, the corresponding event handler is executed.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       METHODS: on_data_changed FOR EVENT data_changed OF lcl_example.

   ENDCLASS.

 

   In this example, `on_data_changed` is an event handler that responds to the `data_changed` event.

 

  1. Constructors

   Constructors are special methods that are automatically called when an object of a class is created. They are used to initialize attributes or perform setup tasks.

   Types of Constructors:

  1.   Instance Constructor: Initializes instance attributes.
  2.  Static Constructor:  Initializes static attributes and is called once before any instance of the class is created.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       METHODS: constructor.

   ENDCLASS.

 

   CLASS lcl_example IMPLEMENTATION.

     METHOD constructor.

       ” Initialization code here

     ENDMETHOD.

   ENDCLASS.

 

   The `constructor` method is the instance constructor used to initialize attributes when an object is created.

 

  1. Interfaces

Interfaces are not a component of a class per se, but they are worth mentioning as they define a contract that a class can implement. An interface specifies a set of methods that the class must implement, promoting code consistency and reusability.

Example:

   INTERFACE lif_example.

     METHODS: add_numbers

       IMPORTING

         iv_num1 TYPE i

         iv_num2 TYPE i

       RETURNING

         VALUE(rv_sum) TYPE i.

   ENDINTERFACE.

 

   A class that implements this interface must define the `add_numbers` method.

 

  1. Aliases

Aliases are used to rename methods inherited from an interface or superclass. This can help resolve naming conflicts or simply make method names more intuitive within the context of the class.

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       INTERFACES: lif_example.

       ALIASES: calculate_sum FOR lif_example~add_numbers.

   ENDCLASS.

 

   Here, `calculate_sum` is an alias for the `add_numbers` method inherited from the `lif_example` interface.

 

Author : Aniket Pawar, 9373518385