0% found this document useful (0 votes)
3 views9 pages

OOP Concepts in ABAP Explained

The document explains key concepts of Object-Oriented Programming (OOP) including classes, objects, encapsulation, inheritance, polymorphism, abstraction, interfaces, constructors, events, type casting, and static methods. Each concept is illustrated with ABAP code examples to demonstrate their implementation. These principles are fundamental for structuring and organizing code in OOP languages.

Uploaded by

Kumar Samrat
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)
3 views9 pages

OOP Concepts in ABAP Explained

The document explains key concepts of Object-Oriented Programming (OOP) including classes, objects, encapsulation, inheritance, polymorphism, abstraction, interfaces, constructors, events, type casting, and static methods. Each concept is illustrated with ABAP code examples to demonstrate their implementation. These principles are fundamental for structuring and organizing code in OOP languages.

Uploaded by

Kumar Samrat
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

✅ 1.

Class & Object

Class: A blueprint or template.

Object: Instance of a class.

Example:

abap

Copy

Edit

CLASS zcl_car DEFINITION.

PUBLIC SECTION.

METHODS: drive.

ENDCLASS.

CLASS zcl_car IMPLEMENTATION.

METHOD drive.

WRITE: 'Driving a car'.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA: car_obj TYPE REF TO zcl_car.

CREATE OBJECT car_obj.

car_obj->drive( ).

✅ 2. Encapsulation

Encapsulation: Hides internal data; only allows access via methods.


Example:

abap

Copy

Edit

CLASS zcl_account DEFINITION.

PRIVATE SECTION.

DATA: balance TYPE i.

PUBLIC SECTION.

METHODS: set_balance IMPORTING amount TYPE i,

get_balance RETURNING VALUE(val) TYPE i.

ENDCLASS.

CLASS zcl_account IMPLEMENTATION.

METHOD set_balance.

balance = amount.

ENDMETHOD.

METHOD get_balance.

val = balance.

ENDMETHOD.

ENDCLASS.

✅ 3. Inheritance

Inheritance: One class (child) inherits from another (parent).

Example:

abap
Copy

Edit

CLASS zcl_person DEFINITION.

PUBLIC SECTION.

METHODS: introduce.

ENDCLASS.

CLASS zcl_person IMPLEMENTATION.

METHOD introduce.

WRITE: 'I am a person'.

ENDMETHOD.

ENDCLASS.

CLASS zcl_employee DEFINITION INHERITING FROM zcl_person.

ENDCLASS.

START-OF-SELECTION.

DATA emp TYPE REF TO zcl_employee.

CREATE OBJECT emp.

emp->introduce( ). "Output: I am a person

✅ 4. Polymorphism

Polymorphism: Same method name behaves differently in child class (overriding).

Example:

abap

Copy

Edit
CLASS zcl_animal DEFINITION.

PUBLIC SECTION.

METHODS: sound.

ENDCLASS.

CLASS zcl_animal IMPLEMENTATION.

METHOD sound.

WRITE: 'Some animal sound'.

ENDMETHOD.

ENDCLASS.

CLASS zcl_dog DEFINITION INHERITING FROM zcl_animal.

PUBLIC SECTION.

METHODS: sound REDEFINITION.

ENDCLASS.

CLASS zcl_dog IMPLEMENTATION.

METHOD sound.

WRITE: 'Dog barks'.

ENDMETHOD.

ENDCLASS.

✅ 5. Abstraction

Abstraction: Hides complex details and only shows essentials using abstract classes.

Example:

abap

Copy
Edit

CLASS zcl_shape DEFINITION ABSTRACT.

PUBLIC SECTION.

METHODS: draw ABSTRACT.

ENDCLASS.

CLASS zcl_circle DEFINITION INHERITING FROM zcl_shape.

PUBLIC SECTION.

METHODS: draw REDEFINITION.

ENDCLASS.

CLASS zcl_circle IMPLEMENTATION.

METHOD draw.

WRITE: 'Drawing a circle'.

ENDMETHOD.

ENDCLASS.

✅ 6. Interfaces

Interface: Only method signatures, no code. Class must implement all methods.

Example:

INTERFACE zif_logger.

METHODS: log.

ENDINTERFACE.

CLASS zcl_console_logger DEFINITION.

PUBLIC SECTION.

INTERFACES: zif_logger.
ENDCLASS.

CLASS zcl_console_logger IMPLEMENTATION.

METHOD zif_logger~log.

WRITE: 'Log to console'.

ENDMETHOD.

ENDCLASS.

✅ 7. Constructor

Constructor: Special method that runs automatically when an object is created.

Example:

abap

Copy

Edit

CLASS zcl_welcome DEFINITION.

PUBLIC SECTION.

METHODS: constructor.

ENDCLASS.

CLASS zcl_welcome IMPLEMENTATION.

METHOD constructor.

WRITE: 'Welcome to OOPs!'.

ENDMETHOD.

ENDCLASS.

✅ 8. Events
Events: One class triggers an event, another class handles it.

Example:

abap

Copy

Edit

CLASS zcl_publisher DEFINITION.

PUBLIC SECTION.

EVENTS: changed.

METHODS: raise_event.

ENDCLASS.

CLASS zcl_publisher IMPLEMENTATION.

METHOD raise_event.

RAISE EVENT changed.

ENDMETHOD.

ENDCLASS.

CLASS zcl_subscriber DEFINITION.

PUBLIC SECTION.

METHODS: on_changed FOR EVENT changed OF zcl_publisher.

ENDCLASS.

CLASS zcl_subscriber IMPLEMENTATION.

METHOD on_changed.

WRITE: 'Event received'.

ENDMETHOD.

ENDCLASS.
✅ 9. Type Casting (Upcasting/Downcasting)

Upcasting: Child → Parent (automatic)

Downcasting: Parent → Child (manual with ?=)

Example:

abap

Copy

Edit

DATA: dog_obj TYPE REF TO zcl_dog,

animal_obj TYPE REF TO zcl_animal.

CREATE OBJECT dog_obj.

animal_obj = dog_obj. "Upcasting

dog_obj ?= animal_obj. "Downcasting

✅ 10. Static Methods and Attributes

Static: Belongs to the class, not to the object.

Example:

abap

Copy

Edit

CLASS zcl_math DEFINITION.


PUBLIC SECTION.

CLASS-METHODS: add IMPORTING a TYPE i b TYPE i RETURNING VALUE(c) TYPE i.

ENDCLASS.

CLASS zcl_math IMPLEMENTATION.

METHOD add.

c = a + b.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

WRITE: zcl_math=>add( 5, 3 ).

You might also like