6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24OOP3008 – Class Definition and Class implementation

In Object-Oriented ABAP (OO ABAP), classes form the foundation of the object-oriented programming paradigm. A class is essentially a blueprint that defines the properties (attributes) and behaviors (methods) of objects. Understanding class definition and implementation is crucial for writing clean, efficient, and reusable code in ABAP. Let’s break down these concepts in simple terms.

What is a Class?

A class is a template or blueprint that defines the structure and behavior of objects. It encapsulates data (attributes) and operations (methods) that can be performed on that data. When you create an object from a class, it inherits all the attributes and methods defined in the class.

Class Definition

Class definition is where you declare what your class will look like—its attributes, methods, and events (if any). Think of it as laying out the blueprint.

 

The class definition is done in the  CLASS… DEFINITION  section. Here’s what it typically includes:

Attributes: Variables that hold the state or data of the class.

Methods: Functions or procedures that define the behavior of the class.

Example of a Class Definition

CLASS lcl_vehicle DEFINITION.

  PUBLIC SECTION.

    ” Attributes

    DATA: brand TYPE string,

          model TYPE string,

          year TYPE i.

 

    ” Methods

    METHODS: display_details,

             start_engine.

ENDCLASS.

 

In this example:

Attributes: `brand`, `model`, and `year` represent the properties of the vehicle.

Methods: `display_details` and `start_engine` define the actions that a vehicle can perform.

 

Class Implementation

Class implementation is where you define the actual code for the methods declared in the class definition. This is where the logic of each method is written.

In ABAP, the class implementation is done in the  CLASS… IMPLEMENTATION  section.

Example of a Class Implementation

CLASS lcl_vehicle IMPLEMENTATION.

  METHOD display_details.

    WRITE: / ‘Brand:’, brand,

           / ‘Model:’, model,

           / ‘Year:’, year.

  ENDMETHOD.

 

  METHOD start_engine.

    WRITE: / ‘The engine has started.’.

  ENDMETHOD.

ENDCLASS.

“`

 

In this example:

display_details: Displays the vehicle’s brand, model, and year.

start_engine: Simply prints a message indicating the engine has started.

 

Bringing It All Together

Once the class is defined and implemented, you can create objects from it and call its methods.

Example of Using the Class

DATA: my_vehicle TYPE REF TO lcl_vehicle.

” Create an instance of the class

CREATE OBJECT my_vehicle.

” Set attribute values

my_vehicle->brand = ‘Toyota’.

my_vehicle->model = ‘Corolla’.

my_vehicle->year = 2020.

” Call methods

my_vehicle->display_details( ).

my_vehicle->start_engine( ).

“`

 

In this example:

– An object `my_vehicle` is created from the `lcl_vehicle` class.

– The object’s attributes are set,  methods are called to display the details and start the engine.

 

Author : Aniket Pawar, 9373518385                                                     

24OOP3008 – Class Definition and Class implementation

In Object-Oriented ABAP (OO ABAP), classes form the foundation of the object-oriented programming paradigm. A class is essentially a blueprint that defines the properties (attributes) and behaviors (methods) of objects. Understanding class definition and implementation is crucial for writing clean, efficient, and reusable code in ABAP. Let’s break down these concepts in simple terms.

What is a Class?

A class is a template or blueprint that defines the structure and behavior of objects. It encapsulates data (attributes) and operations (methods) that can be performed on that data. When you create an object from a class, it inherits all the attributes and methods defined in the class.

Class Definition

Class definition is where you declare what your class will look like—its attributes, methods, and events (if any). Think of it as laying out the blueprint.

 

The class definition is done in the  CLASS… DEFINITION  section. Here’s what it typically includes:

Attributes: Variables that hold the state or data of the class.

Methods: Functions or procedures that define the behavior of the class.

Example of a Class Definition

CLASS lcl_vehicle DEFINITION.

  PUBLIC SECTION.

    ” Attributes

    DATA: brand TYPE string,

          model TYPE string,

          year TYPE i.

 

    ” Methods

    METHODS: display_details,

             start_engine.

ENDCLASS.

 

In this example:

Attributes: `brand`, `model`, and `year` represent the properties of the vehicle.

Methods: `display_details` and `start_engine` define the actions that a vehicle can perform.

 

Class Implementation

Class implementation is where you define the actual code for the methods declared in the class definition. This is where the logic of each method is written.

In ABAP, the class implementation is done in the  CLASS… IMPLEMENTATION  section.

Example of a Class Implementation

CLASS lcl_vehicle IMPLEMENTATION.

  METHOD display_details.

    WRITE: / ‘Brand:’, brand,

           / ‘Model:’, model,

           / ‘Year:’, year.

  ENDMETHOD.

 

  METHOD start_engine.

    WRITE: / ‘The engine has started.’.

  ENDMETHOD.

ENDCLASS.

“`

 

In this example:

display_details: Displays the vehicle’s brand, model, and year.

start_engine: Simply prints a message indicating the engine has started.

 

Bringing It All Together

Once the class is defined and implemented, you can create objects from it and call its methods.

Example of Using the Class

DATA: my_vehicle TYPE REF TO lcl_vehicle.

” Create an instance of the class

CREATE OBJECT my_vehicle.

” Set attribute values

my_vehicle->brand = ‘Toyota’.

my_vehicle->model = ‘Corolla’.

my_vehicle->year = 2020.

” Call methods

my_vehicle->display_details( ).

my_vehicle->start_engine( ).

“`

 

In this example:

– An object `my_vehicle` is created from the `lcl_vehicle` class.

– The object’s attributes are set,  methods are called to display the details and start the engine.

 

Author : Aniket Pawar, 9373518385