ABAP in SAP R/3 ver 4.6 - Object Oriented Language.


REPORT ZVV0001 line-size 80.
*----------------------------------------------------------------------*
*--- Viktor Voilevich                                               ---*
*--- June 2000                                                      ---*
*----------------------------------------------------------------------*
include <LIST>.

*----------------------------------------------------------------------*
types: t_name(15) type c,
       t_icon(4)  type c.
*----------------------------------------------------------------------*
interface i_face.
  methods: m_show,
           m_stat.
  data:    a_total type i,
           a_level type i.
endinterface.
*----------------------------------------------------------------------*
class c_base definition.
  public section.
    methods:    constructor importing value(p_name) type t_name,
                m_append    importing value(p_c)    type ref to c_base,
                m_isme      returning value(p_name) type t_name.
    interfaces  i_face.
  protected section.
    methods:    m_name,
                m_ad,
                m_show.
    data        a_name    type t_name.
    class-data  a_left    type i value 0.
endclass.
*----------------------------------------------------------------------*
class c_derived definition inheriting from c_base.
  public section.
    methods    constructor importing value(p_name) type t_name.
  protected section.
    methods    m_ad redefinition.
endclass.
*----------------------------------------------------------------------*
class c_list definition inheriting from c_derived.
  public section.
    methods:   constructor importing value(p_name) type t_name,
               m_append      redefinition,
               i_face~m_stat redefinition,
               m_num   returning value(p_num) like sy-tabix.
    events     e_total exporting value(p_left) type i.
  protected section.
    methods    m_ad     redefinition.
  private section.
    data       a_c type table of ref to c_base.
endclass.
*----------------------------------------------------------------------*
class c_icon definition inheriting from c_base.
  public section.
    methods    constructor importing value(p_name) type t_name
                                     value(p_icon) type t_icon.
  protected section.
    methods:   m_ad   redefinition,
               m_draw.
  private section.
    data       a_icon type t_icon.
endclass.
*----------------------------------------------------------------------*
class c_total definition.
  public section.
    methods m_total for event e_total of c_list importing p_left sender.
endclass.
*----------------------------------------------------------------------*
class c_base implementation.
  method constructor.
    a_name = p_name.
  endmethod.
  method m_append.
  endmethod.
  method m_isme.
    p_name = a_name.
  endmethod.
  method m_name.
    write at /a_left a_name.
  endmethod.
  method m_ad.
    write: ': I am BASE instance...'.
  endmethod.
  method m_show.
    call method: m_name,
                 m_ad.
  endmethod.
  method i_face~m_show.
    add 2 to a_left.
    call method me->m_show.
    subtract 2 from a_left.
  endmethod.
  method i_face~m_stat.
    add 1 to i_face~a_total.
    if i_face~a_level is initial.
      add 1 to i_face~a_level.
    endif.
  endmethod.
endclass.
*----------------------------------------------------------------------*
class c_derived implementation.
  method constructor.
    call method super->constructor exporting p_name = p_name.
  endmethod.
  method m_ad.
    write: ': I am DERIVED instance...'.
  endmethod.
endclass.
*----------------------------------------------------------------------*
class c_list implementation.
  method constructor.
    call method super->constructor exporting p_name = p_name.
  endmethod.
  method m_append.
    append p_c to a_c.
  endmethod.
  method m_num.
    describe table a_c lines p_num.
  endmethod.
  method m_ad.
    data: w_c type ref to c_base,
          w_i type ref to i_face.
    write ': I am LIST instance... - here are my items:'.
    loop at a_c into w_c.
      new-line.
      w_i = w_c.
      call method w_i->m_show.
    endloop.
    raise event e_total exporting p_left = a_left.
  endmethod.
  method i_face~m_stat.
    data: w_c type ref to c_base,
          w_i type ref to i_face,
          w_level like i_face~a_level.
    call method super->i_face~m_stat.
    if not a_c[] is initial.
      loop at a_c into w_c.
        w_i = w_c.
        call method w_i->m_stat.
        add w_i->a_total to i_face~a_total.
        if w_level < w_i->a_level.
          w_level = w_i->a_level.
        endif.
      endloop.
      add w_level to i_face~a_level.
    endif.
  endmethod.
endclass.
*----------------------------------------------------------------------*
class c_icon implementation.
  method constructor.
    call method super->constructor exporting p_name = p_name.
    a_icon = p_icon.
  endmethod.
  method m_ad.
    write ': I am ICON instance...'.
    call method m_draw.
  endmethod.
  method m_draw.
    write a_icon as icon.
  endmethod.
endclass.
*----------------------------------------------------------------------*
class c_total implementation.
  method m_total.
    data: w_name type t_name,
          w_num  like sy-tabix.
    w_name = sender->m_isme( ).
    w_num  = sender->m_num( ).
    write at /p_left w_name color col_total.
    if w_num > 0.
      write: '--- Total', w_num, 'items.'.
    else.
      write: '--- Is Empty.'.
    endif.
  endmethod.
endclass.
*----------------------------------------------------------------------*
data: i_1 type ref to i_face,
      o_1 type ref to c_base,
      o_2 type ref to c_base,
      o_t type ref to c_total.
*======================================================================*
START-OF-SELECTION.
*----------------------------------------------------------------------*
create object o_1 type c_list exporting p_name = '1st - List'.
create object o_2 type c_icon exporting p_name = '2nd - Icon'
                                        p_icon = ICON_GREEN_LIGHT.
call method   o_1->m_append exporting p_c = o_2.
create object o_2 type c_icon exporting p_name = '3rd - Icon'
                                        p_icon = ICON_POSITIVE.
call method   o_1->m_append exporting p_c = o_2.
create object o_2 type c_list exporting p_name = '4th - List'.
call method   o_2->m_append exporting p_c = o_1.
create object o_1 type c_base exporting p_name = '5th - Base'.
call method   o_2->m_append exporting p_c = o_1.
create object o_1 type c_derived exporting p_name = '6th - Derived'.
call method   o_2->m_append exporting p_c = o_1.
create object o_1 type c_icon exporting p_name = '7th - Icon'
                                        p_icon = ICON_SUM.
call method   o_2->m_append exporting p_c = o_1.
create object o_1 type c_list exporting p_name = '8th - List'.
call method   o_2->m_append exporting p_c = o_1.
create object o_1 type c_list exporting p_name = '9th - List'.
call method   o_1->m_append exporting p_c = o_2.
create object o_2 type c_base exporting p_name = '10th - Base'.
call method   o_1->m_append exporting p_c = o_2.
create object o_2 type c_derived exporting p_name = '11th - Derived'.
call method   o_1->m_append exporting p_c = o_2.
create object o_2 type c_icon exporting p_name = '12th - Icon'
                                        p_icon = ICON_CHECKED.
call method   o_1->m_append exporting p_c = o_2.
create object o_t.
*======================================================================*
END-OF-SELECTION.
*----------------------------------------------------------------------*
i_1 = o_1.
set handler o_t->m_total for all instances.
call method: i_1->m_show, i_1->m_stat.
uline.
write: / 'Total', i_1->a_total, 'objects on', i_1->a_level, 'levels.'.

Program compiled with no errors.
Here is output of the program:

----------------------------------------------------------------------
 9th - List      : I am LIST instance... - here are my items:
   4th - List      : I am LIST instance... - here are my items:
     1st - List      : I am LIST instance... - here are my items:
       2nd - Icon      : I am ICON instance... [ICON]
       3rd - Icon      : I am ICON instance... [ICON]
     1st - List      --- Total          2  items.
     5th - Base      : I am BASE instance...
     6th - Derived   : I am DERIVED instance...
     7th - Icon      : I am ICON instance...
     8th - List      : I am LIST instance... - here are my items:
     8th - List      --- Is Empty.
   4th - List      --- Total          5  items.
   10th - Base     : I am BASE instance...
   11th - Derived  : I am DERIVED instance...
   12th - Icon     : I am ICON instance... [ICON]
 9th - List      --- Total          4  items.
----------------------------------------------------------------------
Total         12  objects on          4  levels.

Therefore, ABAP is 100% Object Oriented Language. Of course, there is some distinctive features and restrictions.
Related Articles:
  1. Jürgen Heymann and Horst Keller. Introducing ABAP Objects.
  2. Horst Keller and Holger Meinert. Inheritance and Interfaces - Polymorphism in ABAP Objects.
  3. Introduction into Object Orientation.


See also:


TopList