Mastering ABAP: Unveiling the Power of OOP Concepts of Class & object
Object oriented programing helps in organizing and structuring code in a more manageable way.
Real Time example about OOP
Car is a class. It defines general features like wheels, doors, and methods like start() and stop().A specific car, like a red Toyota Corolla, is an object of this class. Engine is abstracted. You don’t see how it works, just how to start the car. This hides complexity and shows only what’s necessary. The Dashboard encapsulates all controls (speedometer, fuel gauge) in one place, similar to how a class encapsulates data and methods. ElectricCar inherits from the Car class. It has all the features of a car but adds new ones, like a battery. Driving modes like Sport or Eco mode allow the same car to behave differently based on the mode, illustrating how methods can behave differently in different contexts.
CLASS AND OBJECT — defines the properties (attributes) and behaviors (methods) that the objects created from the class will have. An object, on the other hand, is an instance of a class that contains actual values for the attributes defined in the class.
Real Time Example
The class represents a group of objects having similar properties and behavior. For example, the animal type Dog is a class while a particular dog named Coffee is an object of the Dog class.
SYNTAX for creating Class
CLASS <class_name> DEFINITION.
[PUBLIC|PROTECTED|PRIVATE] SECTION.
DATA: <attribute_name> TYPE <data_type>,
...
METHODS: <method_name> ,
...
ENDCLASS.
CLASS <class_name> IMPLEMENTATION.
"Define methods here
METHOD <method_name>.
"Method implementation code
ENDMETHOD.
...
ENDCLASS.
<class_name>: Name of the class you are creating.
SECTION: Specifies the visibility of the class components (attributes, methods):
PUBLIC SECTION: Accessible from outside the class.
PROTECTED SECTION: Accessible within the class and subclasses.
PRIVATE SECTION: Accessible only within the class itself.
DATA: Used to define attributes (variables) within the class.
METHODS: Used to declare methods (functions) that can be performed by objects of the class.
METHOD: This block is where you implement the logic of the method defined in the class.
DATA: <object_name> TYPE REF TO <class_name>.
CREATE OBJECT <object_name>.
SYNTAX for creating Object
<object_name>: The name of the object (reference variable) you are creating.
TYPE REF TO <class_name>: Specifies that <object_name> is a reference to an object of type <class_name>.
CREATE OBJECT: This statement instantiates the object.
CLASS DEMO1 DEFINITION.
PUBLIC SECTION.
METHODS :
M1.
DATA: NAME TYPE STRING.
ENDCLASS.
CLASS DEMO1 IMPLEMENTATION.
METHOD M1.
WRITE :/ 'NAME :', NAME.
ENDMETHOD.
ENDCLASS.
CLASS STATIC DEFINITION.
PUBLIC SECTION.
CLASS-DATA : COUNTER TYPE I.
CLASS-METHODS : COUNT.
ENDCLASS.
CLASS STATIC IMPLEMENTATION.
METHOD COUNT.
COUNTER += 1.
WRITE : / COUNTER.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: OBJ1 TYPE REF TO DEMO1.
CREATE OBJECT OBJ1.
OBJ1->NAME = 'NILA'.
OBJ1->M1( ).
STATIC=>COUNT( ).
Class DEMO1
Definition:This class has a method M1 and a data attribute NAME.
The method M1 is declared in the PUBLIC SECTION, meaning it can be accessed from outside the class.
The data attribute NAME is of type STRING, also declared in the PUBLIC SECTION.
Implementation:The method M1 is implemented to write the text “NAME:” followed by the value of the NAME attribute to the output.
Class STATIC
Definition:This class has a class-level attribute COUNTER and a class method COUNT.
CLASS-DATA COUNTER: This is a static variable, shared across all instances of the class.
CLASS-METHODS COUNT: This is a static method, which can be called without creating an instance of the class.
Implementation:The method COUNT increments the COUNTER by 1 and then writes its value to the output.
Main Program (START-OF-SELECTION)
Creating Object OBJ1:An object OBJ1 of type DEMO1 is created.
The NAME attribute of OBJ1 is set to “NILA”.
The M1 method of OBJ1 is called, which prints “NAME: NILA” to the output.
Calling Static Method COUNT:The static method COUNT of class STATIC is called directly without creating an object.
This increments the COUNTER by 1 and prints its value.
IMPORTANT POINTS TO NOTE
Instance Method vs. Static Method:
M1 is an instance method, meaning it operates on a specific object (OBJ1).
COUNT is a static method, meaning it can be called on the class itself (STATIC=>COUNT( )).
Instance Attribute vs. Static Attribute:
NAME is an instance attribute, meaning each object of DEMO1 has its own NAME.
COUNTER is a static attribute, meaning it is shared by all instances of the STATIC class (though in this example, no instances are created, and it’s accessed directly via the class).