Friday, March 14, 2025

DAY 1 - Introduction - OOPs Concept, Local Class and Class Components

Why This Blog? 🚀

  • Simplified Learning: OOPs concepts can feel overwhelming, but I’ve broken them down into simple to unsderstand with easy-to-grasp examples.
  • Master the Basics: Once you understand these fundamentals, you'll be able to decode even the most complex object-oriented developments.
  • Enjoy the Journey! Learning OOPs doesn’t have to be complex—let’s make it easy with simple examples!
Let's dive in! 🔥


Pre-Requesites for OOPs In ABAP

You should have a basic understanding of SAP ABAP concepts: - Internal Table, Work Area, Variables, Constants, Etc,

Why OOPs in ABAP

  1. Any new technologies in ABAP like ABAP on HANA, ODATA, Workflow, WebDynpro, BADIs, BAPIs uses OOPs concepts.
  2.  Now a days many company and project requires objects to be developed in OOPs because of its performance is better than traditional ABAP objects.
    • For Example: If you create report1 using FM and report2 using OOPs Concept, report2 has better performance why because while calling FM it trigger FG that will consume more time to process the data
  3. Strong knowlegde in OOPs is must to sustain in indutry

Topic To Be Covered In This Series

    1. Classes, Methods, Attribute
    2. Constructors
    3. Inheritance
    4. Polymorphism
    5. Abstract Class
    6. Interface
    7. Persistence Service
    8. Transaction Service
    9. Casting
    10. Event Handling Design Pattern
__________________________________________________________________________

DAY 1 - Introduction 

OOPs Concept, Local Class and Class Components


Features Of OOPs ABAP

  1. Encapsulation
  2. Data Abstraction
  3. Inheritance
  4. Polymorphism
Encapsulation: Combing all the details into a single unit. Eg: Class

Data Abstraction: Preventing access of the components of the class from outside the class, by using visibility section available in the OOP in ABAP.

    There are three types of visibilitysection:
  1. Public Section - Component under this section can be accessed from the class, from the sub class and also from the outside the class (i.e., by other programs, function modules, or objects).
  2. Protected Section - Component under this section can be accessed from the class, from the sub class but not from the outside the class.
  3. Private Section - Component under this section can be accessed only from the class but not from the sub class and the outside the class.
In ABAP, every component of a class (such as attributes, methods, and events) must be placed under one of the visibility sections. If any component is not assigned to a visibility section, the system will throw an error. In ABAP, there is no default visibility for class components.

Example:

In the following example, the class lcl_class has a variable 'a' that is declared without a visibility section, which will cause a syntax error.
CLASS lcl_class DEFINITION. " Error: Variable 'a' is declared without a visibility section DATA: a TYPE i. ENDCLASS.

To correct this error, the variable a should be declared under one of the visibility sections. For example, if it’s intended to be accessible outside the class, it should be placed under the PUBLIC SECTION:
CLASS lcl_class DEFINITION. PUBLIC SECTION. DATA: a TYPE i. ENDCLASS.

Inheritance: Using the components of one class into another class.

Polymorphism: Same method or function behave differently based on the object calling it. 

There are two types of polymorphism:

  1. Method Overriding (Runtime Polymorphism): A subclass provides a specific implementation of a method already defined in its parent class.
  2. Method Overloading (Compile-time Polymorphism): Multiple methods have the same name but different parameters (though ABAP does not support traditional overloading like Java or C++).
.

Class, Component of Class, Object.

There are two types of classes in ABAP: Local and Global

Global Class: A global class is defined at the global level and can be accessed across different programs, function groups or function modules within the SAP system. Global classes are ideal for reusable components that need to be shared across multiple programs.It can be created from SE24. Naming convention - ZCL<class_name>

Local Class: A local class is defined within a specific program and is only accessible within that program. It can be created in SE38. Naming convention - lcl<class_name>

Syntax:
  1. CLASS <class_name> DEFINITION.
  2. ENDCLASS.
Component of a class:
  1. Attributes - Is nothing but a variable in normal ABAP.
    1. Instance Attribute.
      • Declared using keyword data
      • Can be accessed only by using obj name
      • value of the attribute is specific to the object
      • Syntax:
        • Data <attr_name> type <data_type>.
        • <obj_name>-><attr_name> = <value>
    2. Static Attribute.
      • Declared using keyword class-data
      • Can be accessed using the class name and obj name
      • value of the attribute is common to all the object of the class
      • Syntax:
        • Class-Data <attr_name> type <data_type>.
        • <class_name>=><attr_name> or <obj_name>-><attr_name>
  2. Methods
    1. Instance Method
        • Declared using keyword method
        • SyntaxMETHOD <method_name>.
      • Static Method
        • Declared using keyword class-method
        • SyntaxCLASS-METHOD <method_name>.
      • Constructor/Spl Method
    2. Events
    Attribute:
    Attributes in ABAP are similar to variables in traditional procedural ABAP.There are two types of attributes in ABAP OOP:

    1.Instance Attributes:

    Instance attributes are variables that are associated with a particular instance of a class. Each object (instance) of the class will have its own copy of the instance attributes, and their values are specific to that object.

        • Declared using the DATA keyword.
        • Can be accessed only through an object (using the object's reference).
        • The value of the attribute is specific to each object.
    2.Static Attributes:

    Static attributes are class-level variables, meaning the value of a static attribute is shared among all objects (instances) of the class. Unlike instance attributes, which are unique to each object, static attributes hold a single value that is common across all instances of the class.

        • Declared using the CLASS-DATA keyword.
        • Can be accessed using either the class name (=>) or an object name (->).
        • The value of the attribute is specific to the class (shared by all instances of the class).
    Method:
    • Methods are collections of statements that can be defined once and reused multiple times in different places.
    • In ABAP OOP, methods are conceptually similar to modularization techniques in core ABAP, such as: Includes,Subroutines (FORM),Function Modules
    • Every method must be placed under one of the visibility sections: PUBLIC, PROTECTED, PRIVATE. If not, the system will throw a syntax error.
    • Every method declared in a class must be implemented. If not, the system will raise a syntax error.
    • If method has mandetory parameter, then we must pass value to those parameter while calling the method.

     

    Note:

    1.Three Phases of Methods:

    1. Method Declaration:
      Syntax: METHODS <method_name>.

    2. Method Implementation:
      Syntax: METHOD <method_name> ENDMETHOD.

    3. Method Calling:
      Syntax: CALL METHOD <obj_name>-><method_name>.

    2.There are two types of methods in ABAP OOP:

        • Special Method: Constructor
        • Normal Method:
          • Instance Method Syntax: METHOD <method_name>.
          • Static Method     Syntax: CLASS-METHOD <method_name>.  

    Instance Method:

    1. Instance Methods can access any type of component of a class(Instance, Static, Constant, Type)

    2. Can be called by only object name. ie we cannot call it using class name. 

    3. Instance methods are declared using the METHOD keyword.

    4. Syntax: METHOD <method_name>. 

    Static Method:

    1. Static Methods can only access static component of a class. They cannot access instance components.

    2. Can be called using object name or class name.

    3. Static methods are declared using the CLASS-METHOD keyword.

    4. Syntax: CLASS-METHOD <method_name>.

    Special Method/Constructor:

      1. Constructor special because it is automatically called whenever the object is created or whenever the class loaded into memory.
      2. Constructor are always declared in public section.
      3. Constructor never return a value.
      4. There are two types of constructor.
        1. Instance Constructor - Syntax: METHOD constructor.
        2. Static Constructor     - Syntax: CLASS-METHOD class-constructor

    Instance Constructor

    1. Syntax: METHOD constructor. 
    2. Instance Constructor can only contain importing paramenter and exceptions.
    3. Instance Constructor is specific to each object. 
    4. Instance Constructor executed only once in the life time of the every object.

    Static Constructor


    1. Syntax: CLASS-METHOD class-constructor. 
    2. Static Constructor cannot contain any parameter or exception.
    3. Static Constructor is specific to the class. 
    4. Static Constructor executed only once in the life time of the class.
    5. Static constructor called in two ways
      • Case1: when the first object of the class is created. 
      • Case2: when any static component of the class is accessed before creating the object.

    Note:
    If a class has both an instance constructor and a static constructor, the following sequence occurs when creating objects:

    • When the first object of the class is created, the static constructor is called first, followed by the instance constructor.
    • When a subsequent object is created, only the instance constructor is called, as the static constructor has already been executed during the creation of the first object.

    Example.(Instance Method, Static Method, Instance Cons, Static Cons) 

    REPORT yak_class_mthd_cons.

    CLASS lcl_abc DEFINITION.

     
    PUBLIC SECTION.
       
    "Attribute
       
    DATA: depid TYPE i.              "Instance Attribute
       
    CLASS-DATA: depname(10) TYPE c.  "Static Attribute
       
    "Methods
       
    METHODS m1.                     "Instance Method
       
    CLASS-METHODS m2.               "Static Method

       
    CLASS-DATA: empid TYPE i.
       
    METHODS constructor.            "Instance Constructor
       
    CLASS-METHODS class_constructor."Static Constructor

    ENDCLASS.

    CLASS lcl_abc IMPLEMENTATION.

     
    "Called Automatically when the object is created
     
    METHOD constructor.
        empid
    = 100467.
     
    ENDMETHOD.

     
    METHOD class_constructor.
       
    write: / 'class cons'.
     
    ENDMETHOD.

     
    METHOD m1.
        depid
    = 1001.
        depname
    = 'SAP TECH'.
     
    ENDMETHOD.

     
    METHOD m2.
    *ERROR - Within a static method, only static attributes can be accessed without specifying further information.
    *    WRITE:/ depid.
       
    WRITE:/ depname.
       
    WRITE:/ empid.
     
    ENDMETHOD.

    ENDCLASS.

    START-OF-SELECTION.

     
    DATA: obj_lc TYPE REF TO lcl_abc.
     
    CREATE OBJECT obj_lc.

     
    CALL METHOD obj_lc->m1.
     
    CALL METHOD obj_lc->m2.

    * ERROR - class=>method" can only be specified with static methods.
    * call METHOD lcl_abc=>m1.
     
    CALL METHOD lcl_abc=>m2.

      Method Parameter:

    Types of parameters: Importing, Exporting, Changing, Returning.
  3. Importing: Data passed into the method (obligatory by default, can be made optional).
  4. Exporting: Data returned from the method (always optional).
  5. Changing: Data passed into the method, modified and returned back to the calling program.
  6. Returning: Used to return a single value from the method.
  7.   
     Syntax
    METHOD <method_name> <parameter_type> <parameter_name> TYPE <data_type>. 

    Class Implementation:

    1. In ABAP OOP, when a method is declared in a class, it must be implemented in the class implementation section. Even if no functionality is required, a null implementation (an empty body) must be provided.
    2. You can assign values to the attributes within the method implementation.
    Syntax: 
      1. CLASS <class_name> IMPLEMENTATION.
      2. METHOD <method_name>.
      3. " Method body - Can be empty if no functionality is required
      4. ENDMETHOD.
      5. ENDCLASS.
    Class Instantiation(Object Creation):
     
    Components (such as instance attributes and methods) of a class cannot be accessed outside the class, it will through an error. To access and work with these instance components, you first need to instantiate the class by creating an object.In ABAP, class instantiation refers to the process of creating an instance (or object) of a class. 

    Syntax:
      1. DATA <obj_name> TYPE REF TO <class_name>.
      2. CREATE OBJECT <obj_name>.
    To instantiate a class in ABAP.
        1. You need to create a reference variable that points to an object of the class.
        2. Then use the CREATE OBJECT statement to actually create the object.
    Local Class Syntax:
    1. CLASS <class_name> DEFINITION.
    2. " ----> Componets of a class <----
    3.     Attributes - Is nothing but a variable in normal ABAP
        1. Instance Attribute.
        2. Static Attribute.
    4.    Methods
        1. Instance Method.
        2. Static Menthod.
        3. Constructor/Spl Method.
    5.    Events
    6. " ----> Componets of a class <-----
    7. ENDCLASS.

    8. CLASS <class_name>IMPLEMENTATION.
    9.   METHOD <method_name>.
    10.     " Method body - Can be empty if no functionality is required
    11.   ENDMETHOD.
    12. ENDCLASS. 

    13. DATA <obj_name> TYPE REF TO <class_name>.
    14. CREATE OBJECT <obj_name>.

    15. CALL METHOD <obj_name>-><method_name>.

    Example:(Instance/Static Attribute, Local Class, Objects)

    REPORT yak_class.

    " Class Definition
    CLASS lcl_abc DEFINITION.

     
    PUBLIC SECTION.
       
    DATA: a TYPE i.                " Instance attribute
       
    CLASS-DATA: b TYPE i.          " Static attribute

       
    DATA: empid   TYPE i,           " Employee ID
              empname
    TYPE c LENGTH 20. " Employee name (max length 20)

       
    " Method declarations
       
    METHODS: m1,                    " Method m1 to set employee data
                 m2
    .                    " Method m2 to display employee data

    ENDCLASS.

    " Class Implementation
    CLASS lcl_abc IMPLEMENTATION.

     
    " Method m1 Implementation - Set employee data
     
    METHOD m1.
        empid
    = 100475.                " Assign employee ID
        empname
    = 'YAK'.               " Assign employee name
     
    ENDMETHOD.

     
    " Method m2 Implementation - Display employee data
     
    METHOD m2.
       
    WRITE: / 'Employee Id:', empid,
               /
    'Employee Name:', empname. " Display employee data
     
    ENDMETHOD.

    ENDCLASS.

    START-OF-SELECTION.

     
    " Class Instantiation/Object Creation
     
    DATA: lobj_abc TYPE REF TO lcl_abc.  " Declare object reference for the class
     
    CREATE OBJECT lobj_abc. " Instantiate the class

      lobj_abc
    ->a = 10.                    " Set value for instance attribute 'a'
     
    WRITE: / lobj_abc->a.                " Display value of instance attribute 'a'

      lcl_abc
    =>b = 20.                  " Set value for static attribute 'b' via class name
     
    WRITE: / lcl_abc=>b.              " Display value of static attribute 'b'

     
    " Call methods of the object
     
    CALL METHOD: lobj_abc->m1.           " Call method m1 to set employee data
     
    CALL METHOD: lobj_abc->m2.           " Call method m2 to display employee data


    Example2:


    REPORT yak_employee_management.

    " Class Definition
    CLASS lcl_employee DEFINITION.

     
    PUBLIC SECTION.
       
    DATA: emp_id   TYPE i,            " Employee ID (Instance Attribute)
              emp_name
    TYPE c LENGTH 50, " Employee Name (Instance Attribute)
              emp_dept
    TYPE c LENGTH 30. " Employee Department (Instance Attribute)

       
    CLASS-DATA: emp_count TYPE i.    " Total number of employees (Static Attribute)

       
    " Method Declarations
       
    METHODS: set_employee_data,     " Method to set employee data
                 display_employee_data
    . " Method to display employee data

    ENDCLASS.

    " Class Implementation
    CLASS lcl_employee IMPLEMENTATION.

     
    " Method to Set Employee Data
     
    METHOD set_employee_data.
        emp_id
    = 100475.                    " Set Employee ID
        emp_name
    = 'John Doe'.               " Set Employee Name
        emp_dept
    = 'Finance'.                " Set Employee Department

       
    " Increment Static Attribute (Total Employees)
        emp_count
    = emp_count + 1.
     
    ENDMETHOD.

     
    " Method to Display Employee Data
     
    METHOD display_employee_data.
       
    WRITE: / 'Employee ID: ', emp_id,
               /
    'Employee Name: ', emp_name,
               /
    'Employee Department: ', emp_dept,
               /
    'Total Employees: ', emp_count. " Display Employee Data
     
    ENDMETHOD.

    ENDCLASS.

    START-OF-SELECTION.

     
    " Class Instantiation/Object Creation
     
    DATA: lobj_employee TYPE REF TO lcl_employee.  " Declare object reference for the class
     
    CREATE OBJECT lobj_employee. " Instantiate the class

     
    " Set and Display Employee Data using Methods
     
    CALL METHOD lobj_employee->set_employee_data..   " Set employee data using method
     
    CALL METHOD lobj_employee->display_employee_data. " Display employee data using method

     
    " Accessing Static Attribute
     
    WRITE: / 'Total Employees: ', lcl_employee=>emp_count. " Display total employee count using class name


















      

    No comments:

    Post a Comment

    Day 3 - Persistence Service

      DAY 1 - Introduction - OOPs Concept, Local Class and Class Components Day 2 - Global Class and OOPs Features 1.Persistence Service: In SAP...