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

call methods from different class

Hi

i have doubts regarding access of class method.

my scenario is

i am having method called M1 which is in class A

now i want 2 access that method M1 in Class B which is different class..

how to do that... how to access????

i want clear explanation with syntax.

points wil be rewarded.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Posted on Apr 11, 2008 at 09:43 AM

    >

    > Hi

    > i have doubts regarding access of class method.

    > my scenario is

    >

    > i am having method called M1 which is in class A

    >

    > now i want 2 access that method M1 in Class B which is different class..

    > how to do that... how to access????

    >

    > i want clear explanation with syntax.

    >

    > points wil be rewarded.

    In a method in class B

    * M1 static
    CALL METHOD A=>M1 EXPORTING... IMPORTING...
    " or
    A=>M1( parameters ).
    
    * M1 not static
    " Create instance of A
    DATA: lo_a TYPE REF TO A.
    CREATE OBJECT lo_a EXPORTING...
    
    CALL METHOD A->M1 EXPORTING... IMPORTING
    " or
    A->M1( parameters ).

    Simple, isn't it.

    matt

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 11, 2008 at 08:53 AM

    &----


    *& Report ZOBJ11_ASHISH_A3 *

    *& *

    &----


    *& *

    *& *

    &----


    ______________________________________________________________________

    • Name: Ashish Paliwal

    • Date: 29th February 2008.

    ______________________________________________________________________

    report zobj11_ashish_a3 no standard page heading message-id z11_ap.

    ----


    • CLASS ORGANISATION_2006 DEFINITION

    ----


    *

    ----


    class organisation_2006 definition.

    public section.

    *instance methods declaration

    methods : org_name,

    org_address.

    endclass. "ORGANISATION_2006 DEFINITION

    ----


    • CLASS ORGANISATION_2006 IMPLEMENTATION

    ----


    *

    ----


    class organisation_2006 implementation.

    *instance methods definition

    method org_name.

    write:/5 text-001.

    endmethod. "org_name

    method org_address.

    write:/5 text-002.

    endmethod. "org_address

    endclass. "ORGANISATION_2006 IMPLEMENTATION

    ----


    • CLASS ORGANISATION_2007 DEFINITION

    ----


    • This class inherits CLASS ORGANISATION_2006

    ----


    class organisation_2007 definition inheriting from organisation_2006.

    public section.

    *instance methods declaration

    methods : org_name redefinition,

    rebranding_process.

    endclass. "ORGANISATION_2007 DEFINITION

    ----


    • CLASS ORGANISATION_2007 IMPLEMENTATION

    ----


    *

    ----


    class organisation_2007 implementation.

    *instance methods definition

    method org_name.

    write:/5 text-003.

    endmethod. "org_name

    method rebranding_process.

    write :/5 text-004.

    endmethod. "REBRANDING_PROCESS

    endclass. "ORGANISATION_2007 IMPLEMENTATION

    ----


    • D E C L A R A T I O N O F R E F E R E N C E V A R I A B L E *

    ----


    data : o1 type ref to organisation_2006.

    data : o2 type ref to organisation_2007.

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

    • START-OF-SELECTION *

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

    start-of-selection.

    ----


    • C R E A T I N G O B J E C T S *

    ----


    create object : o1, o2.

    ----


    • C A L L I N G M E T H O D S *

    ----


    call method o1->org_name.

    call method o1->org_address.

    call method o2->org_name.

    call method o2->org_address.

    call method o2->rebranding_process.

    do reward if helpful

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 11, 2008 at 08:59 AM

    Hi checkout this sample code.....any clarrification..really appreciated.

    class lcl_a  DEFINITION.
      PUBLIC SECTION.
      methods: m1.
    endclass.
    
    class lcl_a IMPLEMENTATION.
      method: m1.
        write:/ 'I am method of A class'.
      endmethod.
    endclass.
    
    class lcl_b DEFINITION INHERITING FROM lcl_a.
      PUBLIC SECTION.
      methods: m2.
    endclass.
    
    class lcl_b IMPLEMENTATION.
      method: m2.
    write:/ 'I am method of B Class'.
     endmethod.
    endclass.
    
     data: o_refb type ref to lcl_b.
    
     start-of-SELECTION.
    
     create OBJECT o_refb.
    *You are calling A Class method M1.
     call method  o_refb->M1.
    *You are calling B Class method M2.
     call method  o_refb->m2. 

    Edited by: Anee on Apr 11, 2008 2:46 PM

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Apr 12, 2008 at 03:21 PM

    Hai

    Rehaman,

    it is possible to access the methods defined in one class by methods in another class by using inheritance.

    class my_class1 DEFINITION.

    PUBLIC SECTION.

    methods: m1.

    endclass.

    class my_class1 IMPLEMENTATION.

    method: m1.

    write:/ 'This is m1 method'.

    endmethod.

    endclass.

    class my_class2 DEFINITION INHERITING FROM my_class1.

    PUBLIC SECTION.

    methods: m2.

    endclass.

    class my_class2 IMPLEMENTATION.

    method: m2.

    write:/ 'This is from method m2'.

    endmethod.

    endclass.

    data: obj_ref2 type ref to my_classs2.

    start-of-selection.

    create OBJECT obj_ref2.

    *To call method m1 of my_class1.

    call method obj_ref2->m1.

    *To call method m2 of my_class2.

    call method obj_ref2->m2.

    if useful, reward points.

    Thanks you,

    Prasad G.V.K

    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.