ABAP OOP Concepts
ABAP OOP Concepts
● Definition:
○ Class: A blueprint or template for creating objects. It encapsulates data
(attributes) and behavior (methods).
○ Object: An instance of a class. It represents a specific entity with its own set of
attributes and methods.
Example:
CLASS cl_person DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING iv_name TYPE string,
display_name.
DATA: name TYPE string.
ENDCLASS.
METHOD display_name.
WRITE: / 'Name:', name.
ENDMETHOD.
ENDCLASS.
● Concept: cl_person defines the name attribute and methods to manipulate it. The
display_name method prints the person’s name.
2. Inheritance:
● Definition: Allows one class (subclass) to inherit attributes and methods from another
class (superclass), promoting code reuse and extension.
Example:
CLASS cl_employee DEFINITION INHERITING FROM cl_person.
PUBLIC SECTION.
METHODS: display_employee.
ENDCLASS.
● Concept: cl_employee inherits from cl_person, reusing the name attribute and
display_name method. It adds its own display_employee method.
3. Polymorphism:
Example:
CLASS cl_shape DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: area RETURNING VALUE(rv_area) TYPE f.
ENDCLASS.
METHOD area.
rv_area = 3.14 * radius * radius.
ENDMETHOD.
ENDCLASS.
METHOD area.
rv_area = length * width.
ENDMETHOD.
ENDCLASS.
● Concept: Different shapes (circle and rectangle) implement the area method. The
polymorphic reference lo_shape allows dynamic method calls based on the actual
object type.
4. Encapsulation:
● Definition: Hides the internal state of an object and provides a controlled interface to
interact with it, ensuring data integrity and security.
Example:
CLASS cl_account DEFINITION.
PRIVATE SECTION.
DATA: balance TYPE p DECIMALS 2.
PUBLIC SECTION.
METHODS: deposit IMPORTING amount TYPE p DECIMALS 2,
withdraw IMPORTING amount TYPE p DECIMALS 2,
get_balance RETURNING VALUE(rv_balance) TYPE p DECIMALS 2.
ENDCLASS.
METHOD withdraw.
IF balance >= amount.
balance = balance - amount.
ELSE.
WRITE: / 'Insufficient funds!'.
ENDIF.
ENDMETHOD.
METHOD get_balance.
rv_balance = balance.
ENDMETHOD.
ENDCLASS.
● Concept: The balance attribute is private, and access is controlled via public methods
(deposit, withdraw, get_balance), ensuring encapsulation.
5. Abstraction:
● Definition: Defines abstract classes and methods that provide a template for derived
classes. Abstract classes cannot be instantiated but guide the structure of subclasses.
Example:
CLASS cl_abstract_shape DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: get_area RETURNING VALUE(rv_area) TYPE f.
ENDCLASS.
METHOD get_area.
rv_area = side * side.
ENDMETHOD.
ENDCLASS.
📢 Call to Action:
Ready to enhance your ABAP skills with OOP? Explore this detailed overview on OOP in ABAP
and discover how these concepts can revolutionize your development practices!
Share your experiences with OOP in ABAP, and let’s discuss how these principles have
impacted your projects in the comments below.