0% found this document useful (0 votes)
19 views

ABAP OOP Concepts

Oops

Uploaded by

sohail ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

ABAP OOP Concepts

Oops

Uploaded by

sohail ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

🚀 Mastering Object-Oriented Programming in ABAP: A Comprehensive Guide 🚀

Hello SAP Professionals,

In the ever-evolving world of SAP, Object-Oriented Programming (OOP) stands out as a


transformative paradigm that enhances how we design and manage ABAP applications.
Understanding and leveraging OOP concepts can significantly improve code modularity,
maintainability, and reusability. This comprehensive guide will walk you through the core OOP
principles, their application in ABAP, and practical examples to elevate your programming skills.

🔍 Deep Dive into OOP Concepts in ABAP:


1. Classes and Objects:

● 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.

CLASS cl_person IMPLEMENTATION.


METHOD constructor.
name = iv_name.
ENDMETHOD.

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.

CLASS cl_employee IMPLEMENTATION.


METHOD display_employee.
WRITE: / 'Employee Name:', name.
ENDMETHOD.
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:

● Definition: The ability to process objects of different classes through a common


interface, allowing methods to operate on objects of varying types.

Example:
CLASS cl_shape DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: area RETURNING VALUE(rv_area) TYPE f.
ENDCLASS.

CLASS cl_circle DEFINITION INHERITING FROM cl_shape.


PUBLIC SECTION.
METHODS: constructor IMPORTING iv_radius TYPE f.
PRIVATE SECTION.
DATA: radius TYPE f.
ENDCLASS.

CLASS cl_circle IMPLEMENTATION.


METHOD constructor.
radius = iv_radius.
ENDMETHOD.

METHOD area.
rv_area = 3.14 * radius * radius.
ENDMETHOD.
ENDCLASS.

CLASS cl_rectangle DEFINITION INHERITING FROM cl_shape.


PUBLIC SECTION.
METHODS: constructor IMPORTING iv_length TYPE f iv_width TYPE f.
PRIVATE SECTION.
DATA: length TYPE f,
width TYPE f.
ENDCLASS.
CLASS cl_rectangle IMPLEMENTATION.
METHOD constructor.
length = iv_length.
width = iv_width.
ENDMETHOD.

METHOD area.
rv_area = length * width.
ENDMETHOD.
ENDCLASS.

DATA: lo_shape TYPE REF TO cl_shape,


lv_area TYPE f.

lo_shape = NEW cl_circle( iv_radius = 5 ).


lv_area = lo_shape->area( ).
WRITE: / 'Circle Area:', lv_area.

lo_shape = NEW cl_rectangle( iv_length = 4 iv_width = 6 ).


lv_area = lo_shape->area( ).
WRITE: / 'Rectangle Area:', lv_area.

● 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.

CLASS cl_account IMPLEMENTATION.


METHOD deposit.
balance = balance + amount.
ENDMETHOD.

METHOD withdraw.
IF balance >= amount.
balance = balance - amount.
ELSE.
WRITE: / 'Insufficient funds!'.
ENDIF.
ENDMETHOD.

METHOD get_balance.
rv_balance = balance.
ENDMETHOD.
ENDCLASS.

DATA(lo_account) = NEW cl_account( ).


lo_account->deposit( amount = 100 ).
lo_account->withdraw( amount = 50 ).
WRITE: / 'Current Balance:', lo_account->get_balance( ).

● 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.

CLASS cl_square DEFINITION INHERITING FROM cl_abstract_shape.


PUBLIC SECTION.
METHODS: constructor IMPORTING iv_side TYPE f.
PRIVATE SECTION.
DATA: side TYPE f.
ENDCLASS.

CLASS cl_square IMPLEMENTATION.


METHOD constructor.
side = iv_side.
ENDMETHOD.

METHOD get_area.
rv_area = side * side.
ENDMETHOD.
ENDCLASS.

DATA(lo_square) = NEW cl_square( iv_side = 4 ).


WRITE: / 'Square Area:', lo_square->get_area( ).

● Concept: cl_abstract_shape is abstract and defines a common interface for all


shapes. cl_square provides a concrete implementation of the get_area method.
🌟 Benefits of OOP in ABAP:
1. Modularity: Breaks down complex systems into smaller, manageable pieces, enhancing
code organization and readability.
2. Reusability: Encourages reuse of classes and methods across different applications,
reducing duplication and effort.
3. Maintainability: Encapsulation and abstraction simplify code updates and maintenance,
as changes are localized and do not affect other parts of the system.
4. Flexibility: Polymorphism allows for flexible code that can interact with various object
types, adapting to different scenarios and requirements.

📢 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.

#SAP #ABAP #OOP #Programming #TechInnovation #SoftwareDevelopment

You might also like