The method 'make_a_sound' was overwritten, or better said overridden to act differently from the original class. The same applies to the 'cl_cat' class.

One powerful trait about classes is that we can make more with less and we can also create a common interface to make classes of the same type could act differently. This concept is called 'polymorphism' and a good example for this is the following implementation using the




class cl_animal_factory definition.
  public section.
    class-methods create_animal importing i_animal type i returning value(r_animal) type ref to cl_animal. " Class method, in C# this is called a static method
endclass.

class cl_animal_factory implementation. " Factory pattern
  method create_animal.
    case i_animal.
      when 1.
        data dog type ref to cl_dog.
        create object dog exporting i_dog_name = 'Sparky'.
        r_animal = dog. " It is returned a cl_dog instance.
      when 2.
        data cat type ref to cl_cat.
        create object cat exporting i_cat_name = 'Fluffy'.
        r_animal = cat. " It is returned a cl_cat instance.
      when others.
    endcase.
  endmethod.
endclass.

class cl_introducer definition.
  public section.
    class-methods introduce importing i_animal type ref to cl_animal. " Here the method receives a cl_animal type parameter
endclass.

class cl_introducer implementation.
  method introduce.
    if i_animal is not initial.
      i_animal->introduce_me( ).
    else.
      write / 'I''m nothing'.
    endif.
  endmethod.
endclass.
 
 
 
 
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com