ABAP Objects: Overriding (Redefinition)

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.

Code Lines

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


REPORT  ZOVERRIDING.

*----------------------------------------------------------------------*
* Definition of CAR class
*----------------------------------------------------------------------*
CLASS lcl_car DEFINITION.
  PUBLIC SECTION.
    METHODS: drive,
             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.
    METHODS: drive 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
  DATA: lo_corolla TYPE REF TO lcl_corolla.
  CREATE OBJECT lo_corolla.
  CALL METHOD lo_corolla->drive.
  CALL METHOD lo_corolla->color.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值