Differences between polymorphism in JAVA and ABAP

By Sayantan Chaudhury, Tata Consultancy Services

1.     Introduction: - This tutorial is on differences between polymorphism in JAVA and ABAP.

Definition: -  Polymorphism is an object oriented programming concept to refer to the ability of a variable, function, or object to take on multiple forms. However the concept vary slightly in ABAP and JAVA. The below source codes explains it well using the method sum.

2.     Steps:- 

In ABAP create the Report ZMETHOD_RED_ABAP , class lc_sum_super and lc_sum_sub using the source code in 4.1. In JAVA create the classes class lc_sum_super, lc_sum_sub and main using the source code in 4.2.

3.     Summary:-

Differences

1.

In ABAP we can only redefine (override) a method of super class in sub class, meaning we can change the code logic of the method in subclass but we cannot change the parameter signature ( type, number and order of parameters passed has to be same ).

In JAVA we can both override and overload a method in the same class or sub class. We can change the signature of the method.

2.

In ABAP static methods / class methods cannot be redefined.

In JAVA static methods can be overloaded / overridden redefined.

3.

Runtime polymorphism is supported in ABAP.

Runtime polymorphism is supported in JAVA too.

4.1 Source code:-  polymorphism implementation in ABAP using concept of redefinition

REPORT ZMETHOD_RED_ABAP NO STANDARD PAGE HEADING.

*
*----------------------------------------------------------------------*
* Definition of class SUM_PARENT , parent class
*----------------------------------------------------------------------*
CLASS lc_sum_super DEFINITION.
  
PUBLIC SECTION.
    
DATA v_sum TYPE INTEGER.
    
METHODSsum IMPORTING var1 TYPE integer
                      var2 
TYPE integer
            
EXPORTING var3 TYPE integer.

    
CLASS-METHODS static_sum IMPORTING var1 TYPE integer
                      var2 
TYPE integer
            
EXPORTING var3 TYPE integer.
ENDCLASS.                    "lc_sum_super Definition
*
*----------------------------------------------------------------------*
* Implementation of class SUM
*----------------------------------------------------------------------*
CLASS lc_sum_super IMPLEMENTATION.
  
METHOD sum"instace method sum
    var3 
var1 + var2.
  
ENDMETHOD.
  
METHOD static_sum"static method
    var3 
var1 + var2.
  
ENDMETHOD.
ENDCLASS.                    "lc_sum_super implementation
*
*----------------------------------------------------------------------*
* Definition of SUM_SUBCLASS , sub class
*----------------------------------------------------------------------*
CLASS lc_sum_sub DEFINITION INHERITING FROM lc_sum_super.
  
PUBLIC SECTION.
    
METHODSsum REDEFINITION ."can't change the signature of method
    
"can't redefine static methods
*   CLASS-METHODS : static_sum REDEFINITION .
ENDCLASS.                    "lc_sum_sub implementation
*
*----------------------------------------------------------------------*
* Implementation of SUM_SUBCLASS
*----------------------------------------------------------------------*
CLASS lc_sum_sub IMPLEMENTATION.
  
METHOD sum.
    var3 
var1 + var2 + 10.
  
ENDMETHOD.
ENDCLASS.
*
START-OF-SELECTION.

  
DATA p1 TYPE integer,
         p2 
TYPE integer,
         v_sum 
TYPE integer.

  
DATAobj_super TYPE REF TO lc_sum_super.
  
CREATE OBJECT obj_super.

  p1 
1p2 2v_sum 0.
  
CALL METHOD obj_super->sum "call to sum from super class
                 
EXPORTING var1 P1
                           var2 
P2
                 
IMPORTING var3 v_sum.
  obj_super
->v_sum v_sum.
  
WRITE /'Call to sum from lc_sum_super :',obj_super->v_sum"3

   p1 
1p2 2v_sum 0.
   
"call to static method static_sum
  
CALL METHOD lc_sum_super=>static_sum
                 
EXPORTING var1 P1
                           var2 
P2
                 
IMPORTING var3 v_sum.
  
WRITE /'Call to static_sum  : ',v_sum"3

  
DATAobj_sub TYPE REF TO lc_sum_sub"call to sum in child class
  
CREATE OBJECT obj_sub.

  p1 
1p2 2v_sum 0.
  
CALL METHOD obj_sub->sum
                 
EXPORTING var1 P1
                           var2 
P2
                 
IMPORTING var3 v_sum.
  obj_sub
->v_sum v_sum.
  
WRITE /:'Call to sum from lc_sum_sub : 'obj_sub->v_sum"13

  
" Runtime polymorphism, since obj_super stores obj_sub, the call 
  
"obj_super->v_sum automatically calls sum from sub class
  obj_super 
obj_sub.
  
WRITE /'obj_super calls sum from sub class : ',obj_super->v_sum"13

4.2 Source code:- polymorphism implementation in JAVA using concept of overloading and overriding.

/** super class lc_sum_super */
class lc_sum_super 
{
	/* super class lc_sum_super */
	
	// instance method sum
	int sum(int var1,int var2) 
	{
		int var3;
		var3 = var1 + var2;
		return var3;
	}	
	
	// static method static_sum
	static int static_sum(int var1,int var2) 
	{
		int var3;
		var3 = var1 + var2;
		return var3;
	}
}
/** sub class lc_sum_sub */
class lc_sum_sub extends lc_sum_super
{
	/* subclass class lc_sum_super */
	
	// instance method sum overridden
	int sum(int var1,int var2) 
	{
		int var4;
		var4 = var1 + var2 + 10 ;
		return var4;
	}
	
	//instance method sum overloaded in sub class
	/** one more variables added, signature can be changed */
	int sum(int var1,int var2,int var3) 
		{
			int var4;
			var4 = var1 + var2 + var3;
			return var4;
		}	
	//static methods static_sum overridden
	static int static_sum(int var1,int var2) 
	{
		int var3;
		var3 = var1 + var2 + 10;
		return var3;
	}
}
/** main class */
public class main 
{
	public static void main(String[] args)
	{
		
	int var1 ,var2 ,v_sum,var3;
		
	/* call instance method sum from super class lc_sum_super */
	var1 = 1;var2 = 2;v_sum=0;
	lc_sum_super obj_super = new lc_sum_super();
	obj_super.v_sum = obj_super.sum(var1,var2);
	System.out.println("Call to sum from lc_sum_super : "+obj_super.v_sum); // v_sum = 3
		
	/* call static method static_sum from sub class lc_sum_sub */
	var1 = 1;var2 = 2;v_sum=0;
	v_sum = lc_sum_sub.static_sum(var1,var2);
	System.out.println("Call to static_sum from lc_sum_sub : "+v_sum); // v_sum = 13
		
	/* call instance method sum from sub class lc_sum_sub */
	var1 = 1;var2 = 2;var3=3;v_sum=0;
	lc_sum_sub obj_sub = new lc_sum_sub();
	obj_sub.v_sum = obj_sub.sum(var1,var2,var3);
	System.out.println("Call to sum from lc_sum_sub : "+obj_sub.v_sum); // v_sum = 6
		
	/** Runtime polymorphism, since obj_super stores obj_sub, the call 
	  obj_super->v_sum automatically calls sum from sub class */
	obj_super = obj_sub;
	System.out.println("obj_super calls sum from sub class : "+obj_super.v_sum); 
        // v_sum = 6
	}
}

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