Important Object-Oriented ABAP Interview Questions and Answers with Code | ABAP Insights

 



Important Object-Oriented ABAP Interview Questions and Answers with Code

 1. What is Object-Oriented ABAP? How is it different from procedural ABAP?

Answer: 

Object-Oriented ABAP (OO ABAP) allows developers to model real-world entities as objects in their programs. OO ABAP provides features like inheritance, polymorphism, and encapsulation that procedural ABAP lacks. In procedural ABAP, the focus is on functions and procedures, whereas, in OO ABAP, the focus is on objects and their interactions.

Procedural ABAP:

abap

DATA: lv_value TYPE i.

lv_value = 100.

WRITE: / 'Value is:', lv_value.

Object-Oriented ABAP:

abap

CLASS lcl_example DEFINITION.

  PUBLIC SECTION.

    DATA: lv_value TYPE i.

    METHODS: display.

ENDCLASS.

CLASS lcl_example IMPLEMENTATION.

  METHOD display.

    WRITE: / 'Value is:', lv_value.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  DATA: lo_example TYPE REF TO lcl_example.

  CREATE OBJECT lo_example.

  lo_example->lv_value = 100.

  lo_example->display( ).

 

2. What is a class in Object-Oriented ABAP? How do you create a class?

Answer: 

A class is a blueprint for objects that contains data and behavior in the form of attributes and methods. Classes can be created using the CLASS keyword in ABAP.

Code Example:

abap

CLASS lcl_car DEFINITION.

  PUBLIC SECTION.

    DATA: model TYPE string.

    METHODS: display_model.

ENDCLASS.

CLASS lcl_car IMPLEMENTATION.

  METHOD display_model.

    WRITE: / 'Car Model is:', model.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  DATA: lo_car TYPE REF TO lcl_car.

  CREATE OBJECT lo_car.

  Call method lo_car->model = 'BMW'.

  Call method lo_car->display_model( ).

 

3. Explain the concept of inheritance in Object-Oriented ABAP.

Answer: 

Inheritance allows one class (the subclass) to inherit the properties and methods of another class (the superclass). This promotes code reusability and establishes a hierarchical relationship between classes.

Code Example:

abap

CLASS lcl_vehicle DEFINITION.
  PUBLIC SECTION.
    DATA: speed TYPE i.
    METHODS: set_speed IMPORTING iv_speed TYPE i,
      get_speed RETURNING VALUE(rv_speed) TYPE i.
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
  METHOD set_speed.
    speed = iv_speed.
  ENDMETHOD.
  METHOD get_speed.
    rv_speed = speed.
  ENDMETHOD.
ENDCLASS.
CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle.
  PUBLIC SECTION.
    METHODS: display_speed.
ENDCLASS.

CLASS lcl_car IMPLEMENTATION.
  METHOD display_speed.
    WRITE: / 'Car Speed is:', get_speed( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA: lo_car TYPE REF TO lcl_car.
  CREATE OBJECT lo_car.
  CALL METHOD lo_car->set_speed EXPORTING iv_speed = 100.
  CALL METHOD lo_car->display_speed( ).

 4. What is polymorphism in OO ABAP?

Answer: 

Polymorphism allows one method to have different behaviors based on the object that calls it. It can be achieved through method overriding or interfaces.

Code Example:

abap

CLASS lcl_vehicle DEFINITION.
  PUBLIC SECTION.
    METHODS: move.
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
  METHOD move.
    WRITE: / 'Vehicle is moving'.
  ENDMETHOD.
ENDCLASS.
CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle.
  PUBLIC SECTION.
    METHODS: move REDEFINITION.
ENDCLASS.
CLASS lcl_car IMPLEMENTATION.
  METHOD move.
    WRITE: / 'Car is moving'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA: lo_vehicle TYPE REF TO lcl_vehicle,
        lo_car     TYPE REF TO lcl_car.
  CREATE OBJECT lo_vehicle.
  CREATE OBJECT lo_car.
  CALL METHOD lo_vehicle->move( ).  " Calls the vehicle's move method
  CALL METHOD lo_car->move( ).      " Calls the car's move method

 5. What is encapsulation in Object-Oriented ABAP?

Answer: 

Encapsulation is the practice of hiding data within a class and exposing it only through public methods. This helps in protecting the integrity of data and keeping the class interface clean.

Code Example:

abap

CLASS lcl_bank_account DEFINITION.
  PUBLIC SECTION.
    METHODS: deposit IMPORTING iv_amount TYPE dec5_2,
      get_balance RETURNING VALUE(rv_balance) TYPE dec5_2.
  PRIVATE SECTION.
    DATA: balance TYPE p DECIMALS 2 VALUE 173.
ENDCLASS.
CLASS lcl_bank_account IMPLEMENTATION.
  METHOD deposit.
    balance = balance + iv_amount.
  ENDMETHOD.
  METHOD get_balance.
    rv_balance = balance.
  ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
  DATA: lo_account TYPE REF TO lcl_bank_account,
        lv_balance TYPE p DECIMALS 2.

  CREATE OBJECT lo_account.
  lo_account->deposit( 1000 ).
  lv_balance = lo_account->get_balance( ).
  WRITE: / 'Account Balance:', lv_balance.

 6. What is the difference between instance methods and static methods in OO ABAP?

Answer: 

- Instance Methods: These require an instance of the class (object) to be called. They can access both instance and static attributes.

- Static Methods: These can be called using the class name itself and can only access static attributes.

Code Example:

abap

CLASS lcl_example DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA: static_value TYPE i.
    DATA: instance_value TYPE i.
    CLASS-METHODS: static_method.
    METHODS: instance_method.
ENDCLASS.
CLASS lcl_example IMPLEMENTATION.
  METHOD static_method.
    WRITE: / 'Static Value is:', static_value.
  ENDMETHOD.

  METHOD instance_method.
    WRITE: / 'Instance Value is:', instance_value.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA: lo_example TYPE REF TO lcl_example.

  " Call static method
  lcl_example=>static_value = 10.
  CALL METHOD lcl_example=>static_method( ).
  " Call instance method
  CREATE OBJECT lo_example.
  lo_example->instance_value = 20.
  CALL METHOD lo_example->instance_method( ).
 
*Static Value is:         10
*Instance Value is:         20

7. What are interfaces in OO ABAP, and how do they differ from classes?

Answer: 

Interfaces define a contract that a class must follow, without providing an implementation. Classes can implement multiple interfaces, whereas inheritance in classes is limited to one superclass.

Code Example:

abap

INTERFACE lif_vehicle.
  METHODS: move.
ENDINTERFACE.
CLASS lcl_car DEFINITION.
  PUBLIC SECTION.
    INTERFACES: lif_vehicle.
ENDCLASS.
CLASS lcl_car IMPLEMENTATION.
  METHOD lif_vehicle~move.
    WRITE: / 'Car is moving via interface'.
  ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
  DATA: lo_vehicle TYPE REF TO lif_vehicle,
        lo_car     TYPE REF TO lcl_car.

  CREATE OBJECT lo_car.
  lo_vehicle = lo_car.

  lo_vehicle->move( ).

 

 8. What is the purpose of constructor methods in OO ABAP?

Answer: 

Constructors are special methods that are automatically invoked when an object is created. In ABAP, constructors are used to initialize objects.

Code Example:

abap
CLASS lcl_person DEFINITION.
  PUBLIC SECTION.
    DATA: name TYPE string,
          age  TYPE i.
    METHODS: constructor IMPORTING iv_name TYPE string iv_age TYPE i,
      display.
ENDCLASS.
CLASS lcl_person IMPLEMENTATION.
  METHOD constructor.
    name = iv_name.
    age = iv_age.
  ENDMETHOD.

  METHOD display.
    WRITE: / 'Name:', name, ' Age:', age.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA: lo_person TYPE REF TO lcl_person.
  CREATE OBJECT lo_person EXPORTING iv_name = 'John' iv_age = 30.
  lo_person->display( ).

 

Object-Oriented ABAP provides a robust framework for developing SAP applications that are modular, reusable, and easier to maintain. By mastering concepts such as inheritance, polymorphism, encapsulation, and interfaces, you'll be well-equipped to handle complex programming requirements in SAP environments. Use these interview questions and code examples as a guide to deepen your understanding of OO ABAP.

Post a Comment

Previous Post Next Post