- SAP ABAP Reports
- Report Types and Events
- Message Class
- Example of Classical Report
- Example of Interactive Report
- Example of ALV Report
- Example of Blocked ALV Report
- Example of Hierarchical Report
- Module Pool Programming
- Screen Painter Components
- Events in Flow Logic Editor
- Screen Elements and Creation Steps
- Working with Validations
- Database Operations
- OO Programming in ABAP
- Types of Programming Structure
- Key Features of OO Programming
- Classes and Objects
- Types of Visibility Section
- Class Defination and Implementation
- Object Creation for Class
- Method Declaration and Implementation
- Types of Component Class
- Global Class and implement GLOBAL methods
- OOP's ALV
- OOP's BDC
24OOP2808 – Classes and Objects in OO Programming
What is a Class?
A class in SAP ABAP is like a blueprint used to create objects. It holds data (attributes) and the methods (functions) that operate on this data. Think of a class as a template that defines the structure and behavior for the objects you create from it.
Example:
CLASS zcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: start_engine
stop_engine.
DATA: model TYPE string
color TYPE string.
ENDCLASS.
CLASS zcl_vehicle IMPLEMENTATION.
METHOD start_engine.
WRITE: ‘Engine started.’.
ENDMETHOD.
METHOD stop_engine.
WRITE: ‘Engine stopped.’.
ENDMETHOD.
ENDCLASS.
In this example, zcl_vehicle is a class with two methods (start_engine and stop_engine) and two attributes (model and color).
What is an Object?
An object is an instance of a class. When you create an object, it uses the class as its template. Each object has its own data and can perform the methods defined in the class.
For example:
DATA: vehicle1 TYPE REF TO zcl_vehicle
vehicle2 TYPE REF TO zcl_vehicle.
CREATE OBJECT vehicle1.
CREATE OBJECT vehicle2.
vehicle1->model = ‘Car’.
vehicle1->color = ‘Red’.
vehicle2->model = ‘Bike’.
vehicle2->color = ‘Blue’.
vehicle1->start_engine().
vehicle2->start_engine().
Here, vehicle1 and vehicle2 are objects of the class zcl_vehicle. They each have their own set of attributes (model and color) and can act independently.
Components of a Class in SAP ABAP
A class in SAP ABAP consists of several key components:
- Attributes (Data Components):
– Attributes store the data within an object and define its state.
– Instance Attributes: Specific to each object instance.
– Static Attributes: Shared across all instances of a class.
Example:
DATA: model TYPE string
color TYPE string.
- Methods (Behavior Components):
– Methods define the actions an object can perform. They contain the business logic.
– Instance Methods: Operate on specific object instances.
– Static Methods: Can be called directly using the class name without needing an object.
Example:
METHODS: start_engine
stop_engine.
- Events:
– Events are custom notifications that other objects can respond to.
– Declared using the EVENTS statement and raised within a method.
Example:
EVENTS: engine_started.
- Interfaces:
– Interfaces define a set of methods without implementing them. A class that implements an interface must provide the method implementations.
– Defined using the INTERFACE keyword.
Example:
INTERFACE if_vehicle.
METHODS: start_engine
stop_engine.
ENDINTERFACE.
- Visibility Sections:
– Visibility determines how and where the class’s components can be accessed.
– Public: Accessible by all objects.
– Protected: Accessible by the class itself and its subclasses.
– Private: Accessible only within the class itself.
Example:
CLASS zcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: start_engine
stop_engine.
DATA: model TYPE string.
PRIVATE SECTION.
DATA: color TYPE string.
ENDCLASS.
- Constructor and Destructor:
– The Constructor is automatically called when an object is created and initializes attributes.
– The Destructor is called when an object is destroyed and cleans up resources.
Example:
CLASS zcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: constructor
destructor.
ENDCLASS.
CLASS zcl_vehicle IMPLEMENTATION.
METHOD constructor.
WRITE: ‘Object created.’.
ENDMETHOD.
METHOD destructor.
WRITE: ‘Object destroyed.’.
ENDMETHOD.
ENDCLASS.
Author : Aniket Pawar, 9373518385
24OOP2808 – Classes and Objects in OO Programming
What is a Class?
A class in SAP ABAP is like a blueprint used to create objects. It holds data (attributes) and the methods (functions) that operate on this data. Think of a class as a template that defines the structure and behavior for the objects you create from it.
Example:
CLASS zcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: start_engine
stop_engine.
DATA: model TYPE string
color TYPE string.
ENDCLASS.
CLASS zcl_vehicle IMPLEMENTATION.
METHOD start_engine.
WRITE: ‘Engine started.’.
ENDMETHOD.
METHOD stop_engine.
WRITE: ‘Engine stopped.’.
ENDMETHOD.
ENDCLASS.
In this example, zcl_vehicle is a class with two methods (start_engine and stop_engine) and two attributes (model and color).
What is an Object?
An object is an instance of a class. When you create an object, it uses the class as its template. Each object has its own data and can perform the methods defined in the class.
For example:
DATA: vehicle1 TYPE REF TO zcl_vehicle
vehicle2 TYPE REF TO zcl_vehicle.
CREATE OBJECT vehicle1.
CREATE OBJECT vehicle2.
vehicle1->model = ‘Car’.
vehicle1->color = ‘Red’.
vehicle2->model = ‘Bike’.
vehicle2->color = ‘Blue’.
vehicle1->start_engine().
vehicle2->start_engine().
Here, vehicle1 and vehicle2 are objects of the class zcl_vehicle. They each have their own set of attributes (model and color) and can act independently.
Components of a Class in SAP ABAP
A class in SAP ABAP consists of several key components:
- Attributes (Data Components):
– Attributes store the data within an object and define its state.
– Instance Attributes: Specific to each object instance.
– Static Attributes: Shared across all instances of a class.
Example:
DATA: model TYPE string
color TYPE string.
- Methods (Behavior Components):
– Methods define the actions an object can perform. They contain the business logic.
– Instance Methods: Operate on specific object instances.
– Static Methods: Can be called directly using the class name without needing an object.
Example:
METHODS: start_engine
stop_engine.
- Events:
– Events are custom notifications that other objects can respond to.
– Declared using the EVENTS statement and raised within a method.
Example:
EVENTS: engine_started.
- Interfaces:
– Interfaces define a set of methods without implementing them. A class that implements an interface must provide the method implementations.
– Defined using the INTERFACE keyword.
Example:
INTERFACE if_vehicle.
METHODS: start_engine
stop_engine.
ENDINTERFACE.
- Visibility Sections:
– Visibility determines how and where the class’s components can be accessed.
– Public: Accessible by all objects.
– Protected: Accessible by the class itself and its subclasses.
– Private: Accessible only within the class itself.
Example:
CLASS zcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: start_engine
stop_engine.
DATA: model TYPE string.
PRIVATE SECTION.
DATA: color TYPE string.
ENDCLASS.
- Constructor and Destructor:
– The Constructor is automatically called when an object is created and initializes attributes.
– The Destructor is called when an object is destroyed and cleans up resources.
Example:
CLASS zcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: constructor
destructor.
ENDCLASS.
CLASS zcl_vehicle IMPLEMENTATION.
METHOD constructor.
WRITE: ‘Object created.’.
ENDMETHOD.
METHOD destructor.
WRITE: ‘Object destroyed.’.
ENDMETHOD.
ENDCLASS.
Author : Aniket Pawar, 9373518385