Polymorphism:-
A single entity (methods) behave in multiple ways is called polymorphism.
1.Method Overloading.
2.Method Overriding.
Method Overloading:- It's a process of overloading a method by passing. different no. and types of parameters it's similar to function overloading in C++.
(ABAP doesn't support method overloading).
Method Overriding:-
- It's a process of overriding the superclass method inside subclass.
- Only Public and protected methods can be overwritten.
- whenever a subclass has to overwrite the superclass method inside the subclass has to redeclare the super class method by using the keyword 'REDEFINITION'.
- while redefining the superclass methods we cannot change the visibility.
- The purpose of overriding the superclass method inside the subclass is to provide the additional features which are specific to subclass. In this case it's always recommended to call the superclass method version inside the subclass by using 'SUPER' key.
- SUPER keyword is used for referring to Superclass components from the subclass.
Final Method cannot be Redefined.
A method declared as final can be inherited but cannot be redefined. (Example -2)
Example1:-Polymorphism ( Redefinition and Super)
REPORT ZRDD_OOPS24.
class abc definition.
public section.
methods m1.
endclass.
class abc implementation.
method m1.
write :/ ' inside m1 - super class'.
endmethod.
endclass.
class pqr definition
inheriting from abc.
public section.
methods m1 redefinition.
endclass.
class pqr implementation.
method m1.
write :/ ' inside m1 - sub class'.
call method super->m1.
endmethod.
endclass.
start-of-selection.
write :/ 'SUPER CLASS OBJECT....'.
data ob1 type ref to abc.
create object ob1.
call method ob1->m1.
write :/ 'SUB CLASS OBJECT....'.
data ob2 type ref to pqr.
create object ob2.
call method ob2->m1.
Example 2:
class abc definition.
public section.
methods m1.
endclass.
class abc implementation.
method m1.
write :/ ' inside m1 - super class'.
endmethod.
endclass.
class pqr definition
inheriting from abc.
public section.
methods m1 redefinition.
endclass.
class pqr implementation.
method m1.
write :/ ' inside m1 - sub class'.
call method super->m1.
endmethod.
endclass.
start-of-selection.
write :/ 'SUPER CLASS OBJECT....'.
data ob1 type ref to abc.
create object ob1.
call method ob1->m1.
write :/ 'SUB CLASS OBJECT....'.
data ob2 type ref to pqr.
create object ob2.
call method ob2->m1.
Example 2:
REPORT Z7RDD_OOPS26.
class abc definition.
public section.
methods m1 final.
endclass.
class abc implementation.
method m1.
write :/ 'inside m1 - super class'.
endmethod.
endclass.
class pqr definition inheriting from abc.
* public section.
* methods m1 redefinition.
endclass.
start-of-selection.
data ob type ref to pqr.
create object ob.
call method ob->m1.
class abc definition.
public section.
methods m1 final.
endclass.
class abc implementation.
method m1.
write :/ 'inside m1 - super class'.
endmethod.
endclass.
class pqr definition inheriting from abc.
* public section.
* methods m1 redefinition.
endclass.
start-of-selection.
data ob type ref to pqr.
create object ob.
call method ob->m1.
No comments:
Post a Comment