Skip to Content
author's profile photo Former Member
Former Member

Implementing constructor outside class implementation..

  • 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?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Posted on Jul 20, 2006 at 05:56 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 20, 2006 at 07:21 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 17, 2006 at 10:37 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.