Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

OOABAP Method Redifinition

Former Member
0 Likes
4,125

Hi,

I am doing an example of inheritance where I have class method M1 of C1 class with 1 import parameter.

I have created C2 class by inheriting from C1 and I want to add additional parameter to method M1. Please let me know wether it is possible or not.

If possible please provide me the sample code.

Waiting for your comments.

Krishna.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,379

Hi Krishna,

It is possible to inherit the method and add the additional functionality.

You need to redefine the method.

Please check this link

http://help.sap.com/saphelp_nw70/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htm

CLASS counter DEFINITION.
PUBLIC SECTION.
    METHODS: set IMPORTING value(set_value) TYPE i,
             increment,
             get EXPORTING value(get_value) TYPE i.
  PROTECTED SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS counter IMPLEMENTATION.
  METHOD set.
    count = set_value.
  ENDMETHOD.
  METHOD increment.
    ADD 1 TO count.
  ENDMETHOD.
  METHOD get.
    get_value = count.
  ENDMETHOD.
ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.
PUBLIC SECTION.
    METHODS increment REDEFINITION.
    DATA count_ten.
ENDCLASS.

CLASS counter_ten IMPLEMENTATION.
  METHOD increment.
    DATA modulo TYPE i.
    CALL METHOD super->increment.
    write / count.
    modulo = count mod 10.
    IF modulo = 0.
      count_ten = count_ten + 1.
      write count_ten.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

DATA: count TYPE REF TO counter,
      number TYPE i VALUE 5.

START-OF-SELECTION.

  CREATE OBJECT count TYPE counter_ten.

  CALL METHOD count->set EXPORTING set_value = number.

  DO 20 TIMES.
    CALL METHOD count->increment.
  ENDDO.

Best regards,

raam

7 REPLIES 7
Read only

bpawanchand
Active Contributor
0 Likes
2,379

HI

It is not possible to change the signature of a method in the sub class.You can redefine the super class method but you cannot change SIGNATURE

Regards

Pavan

Edited by: Pavan Bhamidipati on Jul 8, 2008 9:27 AM

Read only

Former Member
0 Likes
2,379

Hi,

When you inherit the class, the parent class public and protected data will be available and you can use.

If the parent class method(M1) contains some code and if u want to add some more lines of code to that same parent class method(M1), it is not possible. You should redefine that method once again to manipulate/change that method in the subclass.

Regards,

Ravi

Read only

Former Member
0 Likes
2,380

Hi Krishna,

It is possible to inherit the method and add the additional functionality.

You need to redefine the method.

Please check this link

http://help.sap.com/saphelp_nw70/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htm

CLASS counter DEFINITION.
PUBLIC SECTION.
    METHODS: set IMPORTING value(set_value) TYPE i,
             increment,
             get EXPORTING value(get_value) TYPE i.
  PROTECTED SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS counter IMPLEMENTATION.
  METHOD set.
    count = set_value.
  ENDMETHOD.
  METHOD increment.
    ADD 1 TO count.
  ENDMETHOD.
  METHOD get.
    get_value = count.
  ENDMETHOD.
ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.
PUBLIC SECTION.
    METHODS increment REDEFINITION.
    DATA count_ten.
ENDCLASS.

CLASS counter_ten IMPLEMENTATION.
  METHOD increment.
    DATA modulo TYPE i.
    CALL METHOD super->increment.
    write / count.
    modulo = count mod 10.
    IF modulo = 0.
      count_ten = count_ten + 1.
      write count_ten.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

DATA: count TYPE REF TO counter,
      number TYPE i VALUE 5.

START-OF-SELECTION.

  CREATE OBJECT count TYPE counter_ten.

  CALL METHOD count->set EXPORTING set_value = number.

  DO 20 TIMES.
    CALL METHOD count->increment.
  ENDDO.

Best regards,

raam

Read only

Former Member
0 Likes
2,379

hi,

This is not possible. As you can change the Functionality of the Method during REDIFINITION but not change the signature of the Method.

Hope this will Clear your Doubt.

Regards

Sumit Agarwal

Read only

Former Member
0 Likes
2,379

Hi,

All subclasses contain the components of all classes between themselves and the root node in an inheritance tree. The visibility of a component cannot be changed. However, you can use the REDEFINITION addition in the METHODS statement to redefine an inherited public or protected instance method in a subclass and make its function more specialized. When you redefine a method, you cannot change its interface. The method retains the same name and interface, but has a new implementation.

The method declaration and implementation in the superclass is not affected when you redefine the method in a subclass. The implementation of the redefinition in the subclass obscures the original implementation in the superclass.

Any reference that points to an object of the subclass uses the redefined method, even if the reference was defined with reference to the superclass. This particularly applies to the self-reference ME->. If, for example, a superclass method M1 contains a call CALL METHOD [ME->]M2, and M2 is redefined in a subclass, calling M1 from an instance of the subclass will cause the original method M2 to be called, and calling M1 from an instance of the subclass will cause the redefined method M2 to be called.

Within a redefine method, you can use the pseudoreference SUPER-> to access the obscured method. This enables you to use the existing function of the method in the superclass without having to recode it in the subclass.

Regards,

Sujit

Read only

Former Member
0 Likes
2,379

Hiii!

You can do this by using redefinition

Try this example in debugging mode



REPORT  YH1146_CLASS_CASTING.

TYPE-POOLS icon.
INCLUDE yh1146_class.

DATA: r_plane TYPE REF TO lcl_airplane,
      r_cargo TYPE REF TO lcl_cargo_plane,
      r_passenger TYPE REF TO lcl_passenger_plane,
      plane_list TYPE TABLE OF REF TO lcl_airplane.

START-OF-SELECTION.

lcl_airplane=>display_n_o_airplanes( ).

CREATE OBJECT r_passenger EXPORTING
                            im_name      = 'LH BERLIN'
                            im_planetype = '747-400'
                            im_seats     = 345.

APPEND r_passenger TO plane_list.

CREATE OBJECT r_cargo EXPORTING
                        im_name      = 'US Hercules'
                        im_planetype = '747-500'
                        im_cargo     = 533.

APPEND r_cargo TO plane_list.

LOOP AT plane_list INTO r_plane.
  r_plane->display_attributes( ).
ENDLOOP.

*&---------------------------------------------------------------------*
*&  Include           YH1146_CLASS                                     *
*&---------------------------------------------------------------------*
CLASS lcl_airplane DEFINITION.
  PUBLIC SECTION.
    CONSTANTS: pos_1 TYPE i VALUE 30.
    METHODS:
      constructor IMPORTING
                    im_name TYPE string
                    im_planetype TYPE saplane-planetype,
                  display_attributes.
    CLASS-METHODS: display_n_o_airplanes.
  PRIVATE SECTION.
    METHODS: get_technical_attributes
               IMPORTING im_type    TYPE saplane-planetype
               EXPORTING ex_weight  TYPE s_plan_wei
                         ex_tankcap TYPE s_capacity.

    DATA: name TYPE string,
          planetype TYPE saplane-planetype.

    CLASS-DATA: n_o_airplanes TYPE i.
ENDCLASS.                    "lcl_airplane DEFINITION

*---------------------------------------------------------------------*
*       CLASS lcl_airplane IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.

  METHOD constructor.
    name      = im_name.
    planetype = im_planetype.
    n_o_airplanes = n_o_airplanes + 1.
  ENDMETHOD.                    "constructor

  METHOD display_attributes.
    DATA: weight TYPE saplane-weight,
             cap TYPE saplane-tankcap.
    WRITE: / icon_ws_plane AS ICON,
           / 'Name of airplane', AT pos_1 name,
           / 'Airplane type:', AT pos_1 planetype.
    get_technical_attributes( EXPORTING im_type    = planetype
                              IMPORTING ex_weight  = weight
                                        ex_tankcap = cap ).
    WRITE: / 'Weight:', weight,
             'Tank capacity:', cap.

  ENDMETHOD.                    "display_attributes

  METHOD display_n_o_airplanes.
    WRITE: /,/ 'Number of airplanes:',AT pos_1 n_o_airplanes
                LEFT-JUSTIFIED, /.
  ENDMETHOD.                    "display_n_o_airplanes

  METHOD get_technical_attributes.
    SELECT SINGLE weight
                  tankcap
             FROM saplane
             INTO (ex_weight, ex_tankcap)
            WHERE planetype = im_type.
    IF sy-subrc <> 0.
      ex_weight = 100000.
      ex_tankcap = 10000.
    ENDIF.
  ENDMETHOD.                    "get_technical_attributes

ENDCLASS.                    "lcl_airplane IMPLEMENTATION

*---------------------------------------------------------------------*
*       CLASS lcl_cargo_plane DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.

  PUBLIC SECTION.

    METHODS: constructor IMPORTING im_name TYPE string
                                   im_planetype TYPE saplane-planetype
                                   im_cargo TYPE scplane-cargomax.

    METHODS: display_attributes REDEFINITION.

    METHODS: get_cargo RETURNING value(re_cargo) TYPE scplane-cargomax.

  PRIVATE SECTION.
    DATA: max_cargo TYPE scplane-cargomax.

ENDCLASS.                    "lcl_cargo_plane DEFINITION

*---------------------------------------------------------------------*
*       CLASS lcl_cargo_plane IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_cargo_plane IMPLEMENTATION.

  METHOD constructor.
    CALL METHOD super->constructor( im_name = im_name
      im_planetype = im_planetype ).
    max_cargo = im_cargo.
  ENDMETHOD.                    "constructor

  METHOD display_attributes.
    super->display_attributes( ).
    WRITE: / 'Max Cargo = ', max_cargo.
    ULINE.
  ENDMETHOD.                    "display_attributes

  METHOD get_cargo.
    re_cargo = max_cargo.
  ENDMETHOD.                    "get_cargo

ENDCLASS.                    "lcl_cargo_plane IMPLEMENTATION

*---------------------------------------------------------------------*
*       CLASS lcl_passenger_plane DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_passenger_plane DEFINITION INHERITING FROM lcl_airplane.

  PUBLIC SECTION.

    METHODS: constructor IMPORTING im_name      TYPE string
                                   im_planetype TYPE saplane-planetype
                                   im_seats     TYPE sflight-seatsmax.

    METHODS: display_attributes REDEFINITION.

  PRIVATE SECTION.
    DATA: max_seats TYPE sflight-seatsmax.



ENDCLASS.                    "lcl_passenger_plane DEFINITION

*---------------------------------------------------------------------*
*       CLASS lcl_passenger_plane IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_passenger_plane IMPLEMENTATION.
  METHOD constructor.
    CALL METHOD super->constructor( EXPORTING im_name = im_name
                                         im_planetype = im_planetype ).
    max_seats = im_seats.
  ENDMETHOD.                    "constructor

  METHOD display_attributes.
    super->display_attributes( ).
    WRITE: / 'Max Seats = ', max_seats.
    ULINE.
  ENDMETHOD.                    "display_attributes

ENDCLASS.                    "lcl_passenger_plane IMPLEMENTATION


Rewards points if useful

Regards

Abhijeet Kulshreshtha

Read only

Former Member
0 Likes
2,379

Hi Krishna,

In OO ABAP method overloading is not supported that means you cannot change the signature of parent class method in child class method in inheritance.

Using method redifinition you can only overwrite the code in child class method but you cannot add your own parameters that means you cannot change the signature of parent class method.

Thanks,

Naveen Kumar.