ABAP Objects: Overriding (Redefinition)

By | September 12, 2008 | ABAP Objects, OO Concepts | 64,380 | 3

Lets check out how we can implement Overriding in ABAP Objects. This is also known as the Redefinition of the method.

When do we need to use the Overriding:

Overriding is useful, when we want to extend the functionality of the inherited method. For example: we have a generic class of CAR and it has method DRIVE. We derived a subclass, say COROLLA, from that class. Now, we need to change the functionality in DRIVE method of the subclass COROLLA. In this situation we can “Redefine” the method DRIVE. This redefinition of the method is called Overriding.

UML

UML diagram for the demo application:

Code Lines

In ABAP, we have extension REDEFINTION of keyword METHODS to be able to implement the Overriding functionality.

*&---------------------------------------------------------------------*
*& Report  ZTEST_NP_OVERRIDING
*&
*&---------------------------------------------------------------------*
REPORT  ztest_overriding.
*
*----------------------------------------------------------------------*
* Definition of CAR class
*----------------------------------------------------------------------*
CLASS lcl_car DEFINITION.
  PUBLIC SECTION.
    METHODSdrive,
             Color.
ENDCLASS.                    "LCL_CAR DEFINITION
*
*----------------------------------------------------------------------*
* Implementation of CAR Class
*----------------------------------------------------------------------*
CLASS lcl_car IMPLEMENTATION.
  METHOD drive.
    WRITE'You are driving a Car'.
  ENDMETHOD.                    "drive
  METHOD COLOR.
    WRITE'Your car has BLUE color'.
  ENDMETHOD.
ENDCLASS.                    "LCL_CAR IMPLEMENTATION
*
*----------------------------------------------------------------------*
* Definition of COROLLA - Inheriting from CAR class
*----------------------------------------------------------------------*
CLASS lcl_corolla DEFINITION INHERITING FROM lcl_car.
  PUBLIC SECTION.
    METHODSdrive REDEFINITION.
ENDCLASS.                    "LCL_COROLLA DEFINITION
*
*----------------------------------------------------------------------*
* Implementation of COROLLA
*----------------------------------------------------------------------*
CLASS lcl_corolla IMPLEMENTATION.
* Here we are overriding the functionality of the drive method.
* We are adding some functionality which are specific to COROLLA class
  METHOD drive.
    CALL METHOD super->drive.
    WRITE'which is Corolla',
           / 'Do you like it?'.
  ENDMETHOD.                    "drive
ENDCLASS.                    "LCL_COROLLA IMPLEMENTATION
*
START-OF-SELECTION.
*
* Object for COROLLA
  DATAlo_corolla TYPE REF TO lcl_corolla.
  CREATE OBJECT lo_corolla.
  CALL METHOD lo_corolla->drive.
  CALL METHOD lo_corolla->color.

Output of this Code snippet will be like:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

3 Comments

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.