Summary -

In this topic, we described about the below sections -

Using inheritance, the methods in the super class can redefine in subclass by using a reference variable that address the superclass objects. The redefined methods can have different implementation in subclass from base class. When the super class addressed, the method has original implementation. When the subclass addressed, the method has a new and different implementation. Using a single reference variable to call the same named methods that are behaving differently called polymorphism.

Polymorphism is a concept of assigning additional or different behaviour or value to the inherited component from the super class that is in the subclass.

The methods with the same name in different subclasses has its own characteristics or implementation. This describes each subclass of the parent class are being explicitly aware that there is a difference exists.

The polymorphism exactly means "many forms". From object oriented prospective, polymorphism allows a single inherited method act differently for each subclass.

Polymorphism comes into the picture when there is inheritance tree created and the classes are related with inheritance. Polymorphism characterstics are -

  • Allows one interface to be used for a general class of actions.
  • Different classes objects behave differently to the same procedural call.
  • User can work with different classes in the same way, regardless of their implementation.
  • Allows code organization and readability as well.
  • The implementation of the method is specific to a class but the form of the address is same.

Example -

Simple example to implement polymorphism in application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_POLYMORPHISM
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_POLYMORPHISM.

* Parent Class definition
CLASS parentclass DEFINITION.
PUBLIC SECTION.

* Parent class method definition.
  METHODS methodnew.
ENDCLASS.

* Parent Class implementation
CLASS parentclass IMPLEMENTATION.

* Method implementation in parent class.
  METHOD methodnew.
     WRITE:/ 'methodnew from parentclass executing..'.
  ENDMETHOD.
ENDCLASS.

* Childclass1 Definition
CLASS Childclass1 DEFINITION INHERITING FROM Parentclass.
PUBLIC SECTION.

* Method definition in Childclass1
  METHODS methodnew REDEFINITION.
ENDCLASS.

* Childclass1 implementation
CLASS childclass1 IMPLEMENTATION.

* Method implementation in Childclass1
  METHOD methodnew.
      WRITE:/ 'methodnew from childclass1 executing..'.
  ENDMETHOD.
ENDCLASS.

* Childclass2 Definition
CLASS Childclass2 DEFINITION INHERITING FROM Parentclass.
PUBLIC SECTION.

* Method definition in Childclass2
  METHODS methodnew REDEFINITION.
ENDCLASS.

* Childclass2 implementation
CLASS childclass2 IMPLEMENTATION.
  METHOD methodnew.

* Method implementation in Childclass2  
      WRITE:/ 'methodnew from childclass2 executing..'.
  ENDMETHOD.
ENDCLASS.

* Creatng class object
START-OF-SELECTION.

* Declaring and creating object for parent class
  DATA: parentobj TYPE REF TO parentclass.
  CREATE OBJECT parentobj.
  WRITE /'Accessing methodnew through parentclass object...'.
  SKIP 2.
  
* Calling method in parent class  
  CALL METHOD: parentobj->methodnew.
  ULINE.

* Declaring and creating object for Childclass1
  DATA: childobj1 TYPE REF TO childclass1.
  CREATE OBJECT childobj1.
  WRITE /'Accessing methodnew through childclass1 object...'.
  SKIP 2.
  
* Calling method in Childclass1    
  CALL METHOD: childobj1->methodnew.
  ULINE.

* Declaring and creating object for Childclass2
  DATA: childobj2 TYPE REF TO childclass2.
  CREATE OBJECT childobj2.
  WRITE /'Accessing methodnew through childclass2 object...'.
  SKIP 2.
  
* Calling method in Childclass2 
  CALL METHOD: childobj2->methodnew.
  ULINE.

Output -

Polymorphism Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

parentclass is the parent defined with method methodnew in the public section and it has its own implementation in the parentclass.

Two child classes: childclass1, childclass2 are inherited from the same parentclass and the methodnew can be accessed from both childclass1 and childclass2. methodnew defined as REDEFINED method in child classes and has implemented differently.

When the method methodnew called from different classes (parentclass, childclass1, and childclass2), it behaves how it is implemented in the specific class. So methodnew is the same method in all the classes and behaves differently when it is called from those classes which is a polymorphism.