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

OOPS Concept

How 2 create local Class in program

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jan 30, 2008 at 10:31 AM

    HI,

    see this example.

    REPORT ZCLASS_EVENT.

    CLASS counter DEFINITION.

    PUBLIC SECTION.

    METHODS increment_counter.

    EVENTS critical_value EXPORTING value(excess) TYPE i.

    PRIVATE SECTION.

    DATA: count TYPE i,

    threshold TYPE i VALUE 10.

    ENDCLASS.

    CLASS counter IMPLEMENTATION.

    METHOD increment_counter.

    DATA diff TYPE i.

    ADD 1 TO count.

    IF count > threshold.

    diff = count - threshold.

    RAISE EVENT critical_value EXPORTING excess = diff.

    ENDIF.

    ENDMETHOD.

    ENDCLASS.

    CLASS handler DEFINITION.

    PUBLIC SECTION.

    METHODS handle_excess

    FOR EVENT critical_value OF counter

    IMPORTING excess.

    ENDCLASS.

    CLASS handler IMPLEMENTATION.

    METHOD handle_excess.

    WRITE: / 'Excess is', excess.

    ENDMETHOD.

    ENDCLASS.

    DATA: r1 TYPE REF TO counter,

    h1 TYPE REF TO handler.

    START-OF-SELECTION.

    CREATE OBJECT: r1, h1.

    SET HANDLER h1->handle_excess FOR ALL INSTANCES.

    DO 20 TIMES.

    CALL METHOD r1->increment_counter.

    ENDDO.

    ***********************************************************************

    REPORT ZCLASS_EVENT.

    CLASS counter DEFINITION.

    PUBLIC SECTION.

    METHODS increment_counter.

    EVENTS critical_value EXPORTING value(excess) TYPE i.

    METHODS handle_excess

    FOR EVENT critical_value OF counter

    IMPORTING excess.

    PRIVATE SECTION.

    DATA: count TYPE i,

    threshold TYPE i VALUE 10.

    ENDCLASS.

    CLASS counter IMPLEMENTATION.

    METHOD increment_counter.

    DATA diff TYPE i.

    ADD 1 TO count.

    IF count > threshold.

    diff = count - threshold.

    RAISE EVENT critical_value EXPORTING excess = diff.

    ENDIF.

    ENDMETHOD.

    METHOD handle_excess.

    WRITE: / 'Excess is', excess.

    ENDMETHOD.

    ENDCLASS.

    DATA: r1 TYPE REF TO counter.

    START-OF-SELECTION.

    CREATE OBJECT: r1.

    SET HANDLER r1->handle_excess FOR ALL INSTANCES.

    DO 20 TIMES.

    CALL METHOD r1->increment_counter.

    ENDDO.

    rgds,

    bharat.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 30, 2008 at 10:31 AM

    hi,

    CLASS lcl_airplane DEFINITION.

    PUBLIC SECTION.

    METHODS: set_name importing

    im_name TYPE string.

    PRIVATE SECTION.

    METHODS: init_name.

    DATA: name TYPE string.

    ENDCLASS.

    CLASS lcl_airplane IMPLEMENTATION.

    METHOD init_name.

    name = ‘No Name‘.

    ENDMETHOD.

    METHOD set_name.

    IF im_name IS INITIAL.

    • Calling init_name

    ...

    ELSE. name = im_name. ENDIF.

    ENDMETHOD.

    ENDCLASS.

    Hope this is helpful, Do reward.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 30, 2008 at 10:34 AM

    Pls refer the pgm: DEMO_ABAP_OBJECTS

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 30, 2008 at 10:37 AM

    hi,

    The components of the class are defined in the definition part. The components are attributes, methods,

    events, constants, types and implemented interfaces. Only methods are implemented in the

    implementation part.

    The CLASS statement cannot be nested, that is, you cannot define a class within a class.

    Attributes describe the data that can be stored in the objects in a class.

    Class attributes can be of any type:

    Data types: scalar (for example, data element), structured, in tables

    ABAP elementary types (C, I, ...)

    Object references

    Interface references

    In classes, you can only use the TYPE reference to refer to data types in the ABAP Dictionary.

    You can only use the LIKE reference for local data objects.

    The READ-ONLY addition means that a public attribute declared with DATA can be read from outside, but

    can only be changed by methods within the class.

    You can currently only use the READ-ONLY addition in the public visibility section (PUBLIC SECTION) of a

    class declaration or in an interface definition.

    You can protect attributes against access from outside by characterizing them as private attributes (defined

    in the PRIVATE SECTION).

    Attributes and their values that may be used directly by an external user are public attributes and are

    defined in the PUBLIC SECTION.

    In the above example for class lcl_airplane, the name attribute is initially defined as a public attribute and

    the weight attribute is defined as a private attribute.

    Public attributes belong to the class ‘external point of contact’ that is, their implementation is publicized. If

    you want to hide the internal implementation from users, you must define internal and external views of

    attributes.

    As a general rule, you should define as few public attributes as possible.

    Hope this is helpful, Do reward.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 30, 2008 at 10:58 AM

    hi

    good

    Global classes are the classes which we find in SE24 and call the methods of them into our program.

    see the sample code

    REPORT zs_class.

    -


    SELECTION SCREEN

    -


    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.

    PARAMETERS:p_var TYPE i,

    p_var1 TYPE i.

    SELECTION-SCREEN END OF BLOCK b1.

    -


    CLASS d_class DEFINITION

    -


    CLASS d_class DEFINITION.

    PUBLIC SECTION.

    METHODS:

    add,

    sub.

    PROTECTED SECTION.

    DATA : var2 TYPE i.

    ENDCLASS. "d_class DEFINITION

    -


    CLASS d_class IMPLEMENTATION

    -


    CLASS d_class IMPLEMENTATION.

    METHOD add.

    var2 = p_var + p_var1.

    WRITE:/ var2.

    ENDMETHOD. "add

    METHOD sub.

    var2 = p_var - p_var1.

    WRITE:/ var2.

    ENDMETHOD. "sub

    ENDCLASS. "d_class IMPLEMENTATION

    -


    START-OF-SELECTION

    -


    START-OF-SELECTION.

    DATA: obj TYPE REF TO d_class.

    CREATE OBJECT: obj .

    CALL METHOD: obj->add,

    thanks

    mrutyun^

    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.