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: 

Dynamically instantiation of an object

matteo_montalto
Contributor
0 Kudos

Hi all,

please forgive my noobiness, I'm a newbie with abap.

I created a "super" class, say A. A contains some methods. Then I declared some classes, say B, C, D, that extend the super class A. These classes inherit from A all the method definition and implementation, and then add two methods that are specific for the single class. These two methods have an identical declarative part (e.g., B, C and D have the same "foo" method, with same parameters, even tho the implementation if it is specific for every class).

I'd like to declare in my report a single object and then, at runtime, decide to instantiate a object from B, C or D dinamically, e.g. depending on certain condition. Is there a way to get this behaviour?

Thanks in advance

3 REPLIES 3

Former Member
0 Kudos

Hi

Perhaps this sample can help you:

parameters: p1(1) type n.

*----------------------------------------------------------------------*
*       CLASS m1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c1 DEFINITION.
  PUBLIC SECTION.
    METHODS: m1.
endclass.                    "m1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c2 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    METHODS: m1 REDEFINITION.
ENDCLASS.                    "c2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c3 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c3 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    METHODS: m1 REDEFINITION.
ENDCLASS.                    "c3 DEFINITION

*----------------------------------------------------------------------*
*       CLASS m1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
  method m1.
    write: 'Class C1'.
  endmethod.                    "m1
ENDCLASS.                    "m1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c2 IMPLEMENTATION.
  method m1.
    write: 'Class C2'.
  endmethod.                    "m1
endclass.                    "c2 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c3 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c3 IMPLEMENTATION.
  METHOD m1.
    write: 'Class C3'.
  ENDMETHOD.                    "m1
ENDCLASS.                    "c3 IMPLEMENTATION

data: my_object  type ref to c1,
      my_object1 type ref to c1,
      my_object2 type ref to c2,
      my_object3 type ref to c3.

start-of-selection.

  create OBJECT my_object1.
  create OBJECT my_object2.
  create OBJECT my_object3.

  case p1.
    when '1'. my_object ?= my_object1.
    when '2'. my_object ?= my_object2.
    when '3'. my_object ?= my_object3.
  endcase.

  call method my_object->m1.

Max

Former Member
0 Kudos

You may try Casting. (down casting)

Regards,

Mohaiyuddin

0 Kudos

or this:

parameters: p1(1) type n.

*----------------------------------------------------------------------*
*       CLASS m1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c1 DEFINITION.
  PUBLIC SECTION.
    METHODS: m1.
endclass.                    "m1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c2 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    METHODS: m1 REDEFINITION.
ENDCLASS.                    "c2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c3 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c3 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    METHODS: m1 REDEFINITION.
ENDCLASS.                    "c3 DEFINITION

*----------------------------------------------------------------------*
*       CLASS m1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c1 IMPLEMENTATION.
  method m1.
    write: 'Class C1'.
  endmethod.                    "m1
ENDCLASS.                    "m1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c2 IMPLEMENTATION.
  method m1.
    write: 'Class C2'.
  endmethod.                    "m1
endclass.                    "c2 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c3 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c3 IMPLEMENTATION.
  METHOD m1.
    write: 'Class C3'.
  ENDMETHOD.                    "m1
ENDCLASS.                    "c3 IMPLEMENTATION

DATA: my_object  TYPE REF TO object,
      my_object1 TYPE REF TO c1,
      my_object2 TYPE REF TO c2,
      my_object3 TYPE REF TO c3.

data: v_class(2) type c,
      meth(2) type c value 'M1'.

start-OF-selection.

  case p1.
    when '1'. v_class = 'C1'.
    when '2'. v_class = 'C2'.
    when '3'. v_class = 'C3'.
  endcase.

  CREATE OBJECT my_object type (v_class).

  call method my_object->(meth).

max