SAP ABAP : OOPS Concept - Classes And Objects
Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities.
Class
- Modifiers: A class can be public or has default access.
- Class keyword: class keyword is used to create a class.
- Class name: The name should begin with an lcl for local class and zcl for Global class.
- Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword INHERITING FROM. A class can only INHERIT (subclass) one parent.
- Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
- Body: The class body surrounded by Class and Endclass.

Object
- State: It is represented by attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
Example of an object: dog


PUBLIC SECTION. "access modifier
DATA name TYPE char10."attributes
METHODS m_eat . "behaviours
ENDCLASS.
"there is two way to create object of class after release 7.4
METHOD m_eat.
ENDMETHOD.
ENDCLASS.
Initializing an object
The new operator or create object instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator or create object also invokes the class constructor.
REPORT ztest_demo_sup.
class lcl_animal DEFINITION. "class declaration
PUBLIC SECTION. "access modifier
DATA name TYPE char10."attributes
METHODS constructor IMPORTING name TYPE char10.
METHODS m_eat . "behaviours
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal. "declaring object of class
"there is two way to create object of class after release 7.4
CREATE OBJECT lv_dog EXPORTING name = 'Milo'. "or
DATA(lv_cat) = NEW lcl_animal( name = 'Kitty')."incline declaration with New keyword "supported only release after 7.4 ve
CLASS lcl_animal IMPLEMENTATION.
METHOD constructor.
WRITE : / name.
ENDMETHOD.
METHOD m_eat.
ENDMETHOD.
ENDCLASS.
Comments
Post a Comment