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

ABAP object

Former Member
0 Likes
776

do i create function overloading or operator overloading into the

abap objects.

if yes then how ?

4 REPLIES 4
Read only

Former Member
0 Likes
646

you can use with redefining method in sub class... use key word redefinition...

thxxx

Read only

Former Member
0 Likes
646

abap does not support operator overlading

it support function overloading...also called inheritence

INHERITING FROM

CLASS subclass DEFINITION INHERITING FROM superclass. ... ENDCLASS.

It therefore involves a simple addition to the CLASS DEFINITION statement. Any non-final class that is visible at this point can be specified for superclass. To create a subclass in the Class Builder, select Superclass on the Properties tab page. Then enter any non-final, global class as a superclass in the Inherits from field. The Undo inheritance and Change inheritance options allow you to change the inheritance relationship

Simple Example of InheritanceREPORT z_inheritance.

CLASS demo DEFINITION.

PUBLIC SECTION.

CLASS-METHODS main.

ENDCLASS.

CLASS vehicle DEFINITION.

PUBLIC SECTION.

METHODS: accelerate IMPORTING delta TYPE i,

show_speed.

PROTECTED SECTION.

DATA speed TYPE i.

ENDCLASS.

CLASS car DEFINITION INHERITING FROM vehicle.

ENDCLASS.

CLASS truck DEFINITION INHERITING FROM vehicle.

PUBLIC SECTION.

METHODS: load IMPORTING freight TYPE string,

unload.

PROTECTED SECTION.

DATA freight TYPE string.

ENDCLASS.

CLASS vehicle IMPLEMENTATION.

METHOD accelerate.

me->speed = me->speed + delta.

ENDMETHOD.

METHOD show_speed.

DATA output TYPE string.

output = me->speed.

MESSAGE output TYPE 'I'.

ENDMETHOD.

ENDCLASS.

CLASS truck IMPLEMENTATION.

METHOD load.

me->freight = freight.

ENDMETHOD.

METHOD unload.

CLEAR me->freight.

ENDMETHOD.

ENDCLASS.

CLASS demo IMPLEMENTATION.

METHOD main.

DATA: car_ref TYPE REF TO car,

truck_ref TYPE REF TO truck.

CREATE OBJECT: car_ref,

truck_ref.

car_ref->accelerate( 130 ).

car_ref->show_speed( ).

truck_ref->load( `Beer` ).

truck_ref->accelerate( 110 ).

truck_ref->show_speed( ).

truck_ref->unload( ).

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

demo=>main( ).

The vehicle class contains a protected attribute (speed) and two public methods (accelerate and show_speed). Note that we have explicitly specified that vehicle inherits from object. Normally, we do not specify the INHERITING addition for such classes. The car and truck classes are both derived from vehicle. Therefore, they inherit the attribute and methods of the vehicle class. Since speed is declared in the PROTECTED SECTION, it is also visible in the subclasses. The truck class is specialized with an additional attribute for freight and additional methods for loading and unloading (load and unload). In this example, the car class receives no additional components. This means that its objects are the same as those of the vehicle class. Since no methods have been added, car does not require an implementation part.

In the main method of the demo class, we use the reference variables car_ref und truck_ref to generate one object each for the two subclasses and call their methods. The accelerate and show_speed methods can be used in both subclasses; however, the load and unload methods can be used only in truck.

The syntax for redefining an instance method in a subclass is as follows:

METHODS meth REDEFINITION.

This statement must be specified in the declaration part of the subclass in the same visibility section as the actual declaration of the method in the superclass. The definition of the interface is not repeated.

In the Class Builder, you redefine an inherited method by displaying it on the Methods tab. To do so, you must use the Settings function of the Class Builder to select the Display Inherited Components Also entry. Then, you must highlight the method and select the Redefine function

Method RedefinitionREPORT z_method_redefinition.

...

CLASS car DEFINITION INHERITING FROM vehicle.

PUBLIC SECTION.

METHODS show_speed REDEFINITION.

ENDCLASS.

CLASS truck DEFINITION INHERITING FROM vehicle.

PUBLIC SECTION.

METHODS: accelerate REDEFINITION,

show_speed REDEFINITION,

load IMPORTING freight TYPE string,

unload.

PROTECTED SECTION.

DATA freight TYPE string.

PRIVATE SECTION.

CONSTANTS max_speed TYPE i VALUE '80'.

ENDCLASS.

...

CLASS car IMPLEMENTATION.

METHOD show_speed.

DATA output TYPE string.

output = me->speed.

CONCATENATE `Car, speed: ` output INTO output.

MESSAGE output TYPE 'I'.

ENDMETHOD.

ENDCLASS.

CLASS truck IMPLEMENTATION.

METHOD accelerate.

super->accelerate( delta ).

IF me->speed > truck=>max_speed.

me->speed = truck=>max_speed.

ENDIF.

ENDMETHOD.

METHOD show_speed.

DATA output TYPE string.

output = me->speed.

CONCATENATE `Truck with `

me->freight

`, speed: `

output

INTO output.

MESSAGE output TYPE 'I'.

ENDMETHOD.

...

ENDCLASS.

CLASS demo IMPLEMENTATION.

METHOD main.

DATA: car_ref TYPE REF TO car,

truck_ref TYPE REF TO truck.

CREATE OBJECT: car_ref,

truck_ref.

car_ref->accelerate( 130 ).

car_ref->show_speed( ).

truck_ref->load( `Beer` ).

truck_ref->accelerate( 110 ).

truck_ref->show_speed( ).

truck_ref->unload( ).

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

demo=>main( ).

Thanks

Please reward if useful

Richa khosla

Edited by: Richa Khosla on May 26, 2008 11:31 AM

Edited by: Richa Khosla on May 26, 2008 11:33 AM

Read only

Former Member
0 Likes
646

Hi,

redefinition of method is possible in abap objects.

see this example.


CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS m1 IMPORTING p1 TYPE string.
  PRIVATE SECTION.
    DATA a1 TYPE string VALUE `c1: `.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    METHODS m1 REDEFINITION.
  PRIVATE SECTION.
    DATA a1 TYPE string VALUE `c2: `.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD m1.
    CONCATENATE a1 p1 INTO a1.
    WRITE / a1.
  ENDMETHOD.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
  METHOD m1.
    super->m1( p1 ).
    CONCATENATE a1 p1 INTO a1.
    WRITE / a1.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA oref TYPE REF TO c1.

CREATE OBJECT oref TYPE c2.
oref->m1( `...` ).

rgds,

bharat.

Read only

0 Likes
646

hi guru

that thing in know but is it possible like C++.