SAP Virtual/Onsite Trainings

Document Categories:

What's New?

Contribute?

Sample Specs

What's Hot?

Web Dynpro for ABAP Tutorials 


Join the Mailing List
Enter name and email address below:
Name:
Email:
Subscribe  Unsubscribe 

Creating table of References to invoke methods of different subclasses

By Ankit Doshi, TCS

This is a simple tutorial showing how we can create table of references and taking the advantage of polymorphism concept we can invoke the different implementation of the same method of the different subclasses using a single object.

Example:

Super class definition of Class animal.  Animal being a generic class will not have any behaviour and hence is declared as abstract.

Subclass Dog which inherits from class animal and creates its own implementation for the method make_noise.

Subclass Dog which inherits from class animal and creates its own implementation for the method make_noise.

Now we create a table of references of type animal and we will use this reference type to invoke the methods of both dog and cat class.

Now taking the advantage of polymorphism the super class being unaware of the implementation of the method make_noise can we can use the super class reference to invoke the methods of sub class.

Output:

Code :

*&---------------------------------------------------------------------*
*& Report  YCL_POLYMORPHISM
*&
*&---------------------------------------------------------------------*

REPORT  ycl_polymorphism.

*----------------------------------------------------------------------*
*       CLASS animal DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS animal DEFINITION ABSTRACT.

  PUBLIC SECTION.
    DATA : noise TYPE char10.
    METHODS make_noise.

ENDCLASS.                    "animal DEFINITION

*----------------------------------------------------------------------*
*       CLASS animal IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS animal IMPLEMENTATION.

  METHOD make_noise.
  ENDMETHOD.                    "make_noise

ENDCLASS.                    "animal IMPLEMENTATION


*----------------------------------------------------------------------*
*       CLASS dog DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS dog DEFINITION INHERITING FROM animal.

  PUBLIC SECTION.
    DATA play TYPE char10.
    METHODS make_noise REDEFINITION.

ENDCLASS.                    "dog DEFINITION

*----------------------------------------------------------------------*
*       CLASS dog IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS dog IMPLEMENTATION.

  METHOD make_noise.
    noise  = 'Bark'.
    play  = 'Wag tail'.
    WRITE / : noise,
              play.
    SKIP.
  ENDMETHOD.                    "make_noise

ENDCLASS.                    "dog IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cat DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cat DEFINITION INHERITING FROM animal.

  PUBLIC SECTION.

    METHODS make_noise REDEFINITION.

ENDCLASS.                    "cat DEFINITION

*----------------------------------------------------------------------*
*       CLASS cat IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cat IMPLEMENTATION.

  METHOD make_noise.
    noise = 'Meow'.
    WRITE / noise.
    SKIP.
  ENDMETHOD.                    "make_noise

ENDCLASS.                    "cat IMPLEMENTATION


START-OF-SELECTION.

  DATA : ref_animal TYPE TABLE OF REF TO animal,
         ref_dog    TYPE REF TO dog,
         ref_cat    TYPE REF TO cat.

  FIELD-SYMBOLS <fs_animal> LIKE LINE OF ref_animal.
  DATA wa_animal LIKE LINE OF ref_animal.

  CREATE OBJECT ref_dog.
  CREATE OBJECT ref_cat.

  wa_animal = ref_dog.
  APPEND wa_animal TO ref_animal.

  wa_animal = ref_cat.
  APPEND wa_animal TO ref_animal.

  wa_animal = ref_dog.
  APPEND wa_animal TO ref_animal.

  LOOP AT ref_animal
    ASSIGNING <fs_animal>.
    CALL METHOD <fs_animal>->make_noise.
  ENDLOOP.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image