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!
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
- Any new technologies in ABAP like ABAP on HANA, ODATA, Workflow, WebDynpro, BADIs, BAPIs uses OOPs concepts.
- 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
- Strong knowlegde in OOPs is must to sustain in indutry
Topic To Be Covered In This Series
- Classes, Methods, Attribute
- Constructors
- Inheritance
- Polymorphism
- Abstract Class
- Interface
- Persistence Service
- Transaction Service
- Casting
- Event Handling Design Pattern
DAY 1 - Introduction
OOPs Concept, Local Class and Class Components
Features Of OOPs ABAP
- Encapsulation
- Data Abstraction
- Inheritance
- Polymorphism
- 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).
- Protected Section - Component under this section can be accessed from the class, from the sub class but not from the outside the class.
- Private Section - Component under this section can be accessed only from the class but not from the sub class and the outside the class.
Example:
lcl_class
has a variable 'a'
that is declared without a visibility section, which will cause a syntax error.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:There are two types of polymorphism:
- Method Overriding (Runtime Polymorphism): A subclass provides a specific implementation of a method already defined in its parent class.
- 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 <class_name> DEFINITION.
- ENDCLASS.
- Attributes - Is nothing but a variable in normal ABAP.
- 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>
- 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>
- Methods
- Instance Method
- Declared using keyword method
- Syntax:
METHOD <method_name>.
- Static Method
- Declared using keyword class-method
- Syntax: CLASS-
METHOD <method_name>.
- Constructor/Spl Method
- Events
Attributes in ABAP are similar to variables in traditional procedural ABAP.There are two types of attributes in ABAP OOP:Attribute:
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:
Method Declaration:
Syntax:METHODS <method_name>.
Method Implementation:
Syntax:METHOD <method_name> ENDMETHOD.
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:
Instance Methods can access any type of component of a class(Instance, Static, Constant, Type)
Can be called by only object name. ie we cannot call it using class name.
Instance methods are declared using the METHOD keyword.
Syntax: METHOD <method_name>.
Static Method:
Static Methods can only access static component of a class. They cannot access instance components.
Can be called using object name or class name.
Static methods are declared using the CLASS-METHOD keyword.
Syntax: CLASS-METHOD <method_name>.
Special Method/Constructor:
- Constructor special because it is automatically called whenever the object is created or whenever the class loaded into memory.
- Constructor are always declared in public section.
- Constructor never return a value.
- There are two types of constructor.
- Instance Constructor - Syntax: METHOD constructor.
- Static Constructor - Syntax: CLASS-METHOD class-constructor
Instance Constructor
- Syntax: METHOD constructor.
- Instance Constructor can only contain importing paramenter and exceptions.
- Instance Constructor is specific to each object.
- Instance Constructor executed only once in the life time of the every object.
Static Constructor
- Syntax: CLASS-METHOD class-constructor.
- Static Constructor cannot contain any parameter or exception.
- Static Constructor is specific to the class.
- Static Constructor executed only once in the life time of the class.
- 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.Importing: Data passed into the method (obligatory by default, can be made optional). Exporting: Data returned from the method (always optional). Changing: Data passed into the method, modified and returned back to the calling program. Returning: Used to return a single value from the method. Syntax:METHOD <method_name> <parameter_type> <parameter_name> TYPE <data_type>.
Class Implementation:
- 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.
- You can assign values to the attributes within the method implementation.
Syntax:
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:
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