Application Development 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: 

Implementing constructor outside class implementation..

Former Member
0 Kudos
175
  • REPORT ZTUSH.

CLASS counter DEFINITION.

PUBLIC SECTION.

METHODS CONSTRUCTOR.

CLASS-METHODS: set IMPORTING value(set_value) TYPE i,

increment,

get EXPORTING value(get_value) TYPE i.

PRIVATE SECTION.

CLASS-DATA count TYPE i.

ENDCLASS.

METHOD CONSTRUCTOR.

WRITE:/ 'I AM CONSTRUCTOR DUDE'.

ENDMETHOD.

CLASS counter IMPLEMENTATION.

METHOD set.

count = set_value.

ENDMETHOD.

ENDCLASS.

DATA cnt TYPE REF TO counter.

START-OF-SELECTION.

CREATE OBJECT cnt.

CALL METHOD counter=>set EXPORTING set_value = number.

I THOUGHT WE CAN DEFINE CONSTRUCTOR METHOD OUTSIDE CLASS IMPLEMENTATION AS IN JAVA. But when I do that I get an error, method can be implemented only withing class. Why?

3 REPLIES 3

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
94

This is the design in ABAP OO. You must implement the Constructor as well as all methods in the Implementation section of the class.

REgards,

Rich Heilman

Former Member
0 Kudos
94

Hi Rajesh,

No you can not define CONSTRUCTOR METHOD OUTSIDE CLASS IMPLEMENTATION because in all public class the methods are defined in it events of our mouse defined in your local class such as :

data: editor TYPE REF TO cl_gui_textedit.

Here cl_gui_textedit is public class for editor when you dobleclick on it you see all methods.I think you just check that your container is not initial when loop run 1st time it will not getting value,Than please check your SET hendler.

Regards.

Ankur Garg.

Message was edited by: Ankur Garg

Message was edited by: Ankur Garg

uwe_schieferstein
Active Contributor
0 Kudos
94

Hello Rajesh

I do not fully understand what you mean by "I THOUGHT WE CAN DEFINE CONSTRUCTOR METHOD OUTSIDE CLASS IMPLEMENTATION AS IN JAVA". However, if you mean that we can create an object without having an explicit CONSTRUCTOR method defined then this is possible in ABAP like in Java (see coding below).

Regards

Uwe

REPORT ztush.


*---------------------------------------------------------------------*
*       CLASS counter DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS counter DEFINITION.
  PUBLIC SECTION.
*METHODS CONSTRUCTOR.
    CLASS-METHODS: set IMPORTING value(set_value) TYPE i,
    increment,
    get EXPORTING value(get_value) TYPE i.
  PRIVATE SECTION.
    CLASS-DATA count TYPE i.

* NO explicit constructor
*METHOD CONSTRUCTOR.
*WRITE:/ 'I AM CONSTRUCTOR DUDE'.
*ENDMETHOD.
ENDCLASS.                    "counter DEFINITION


*---------------------------------------------------------------------*
*       CLASS counter IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS counter IMPLEMENTATION.

  METHOD set.
    count = set_value.
  ENDMETHOD.                    "set


  METHOD get.
  ENDMETHOD.                    "get

  METHOD increment.
  ENDMETHOD.                    "increment
ENDCLASS.                    "counter IMPLEMENTATION


DATA cnt TYPE REF TO counter.


START-OF-SELECTION.
* Implicit constructor is called
  CREATE OBJECT cnt.

  CALL METHOD counter=>set
    EXPORTING
      set_value = 5.

END-OF-SELECTION.