Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

poly

Former Member
0 Likes
733

can any some detailed information,faq's regarding only polymorphism,not other than that.I will be very thankful to those who have done it.

3 REPLIES 3
Read only

Former Member
0 Likes
709

Hi,

FAQ's on polymorphism

1. What is Polymorphism?

2. What is dynamic binding of objects

3. Can a reference variables declared with static reference to a super class can dynamically point to an object of a subclass of this super class and access the components known to the super class.

4. How is polymorphism is used in interfaces?

5. Do Interfaces allow to use different classes in a uniform way using interface references?

Hope you will find these faq's useful.

Regards,

Kate

Read only

Former Member
0 Likes
709

Hi Sandeep,

<u><b>Inheritance & Polymorphism</b></u>

Redefining methods introduces POLYMORPHISM in classes. When objects of different classes (e.g. superclass & subclass) react differently to the same method calls, that is called polymorphism.

A client can handle different classes uniformly, irrespective of their

implementation. The runtime system searches for the right

implementation of a method on behalf of the client.

Use – To write programs that are highly generic, that is, they do not need

to be changed significantly if use cases are added.

<u><b>SAMPLE CODE</b></u>

&----


*& Class (Definiton) zl_lcl_vehicle

&----


  • Definition of the Superclass

----


CLASS zl_lcl_vehicle DEFINITION.

PUBLIC SECTION.

  • Visible to all

METHODS: set_make IMPORTING im_make TYPE string

im_model TYPE string,

get_make EXPORTING ex_make TYPE string

ex_model TYPE string.

PROTECTED SECTION.

  • Visible within class and all subclasses

DATA gv_n_o_veh TYPE i.

PRIVATE SECTION.

  • Only visible within the class

DATA: gv_make TYPE string, " Vehicle make

gv_model TYPE string. " Type or model

ENDCLASS. "zl_lcl_vehicle DEFINITION

&----


*& Class (Implementation) zl_lcl_vehicle

&----


  • Implementation of the Superclass

----


CLASS zl_lcl_vehicle IMPLEMENTATION.

METHOD set_make.

IF im_make IS NOT INITIAL

AND im_model IS NOT INITIAL.

gv_make = im_make.

gv_model = im_model.

ENDIF.

ENDMETHOD. "set_make

METHOD get_make.

ex_make = gv_make.

ex_model = gv_model.

ENDMETHOD. "get_make

ENDCLASS. "zl_lcl_vehicle

&----


*& Class (Definiton) zl_lcl_car

&----


  • Definition of the Subclass

----


CLASS zl_lcl_car DEFINITION INHERITING FROM zl_lcl_vehicle.

PUBLIC SECTION.

METHODS: set_vehtype IMPORTING im_vehtype TYPE string

im_n_o_veh TYPE i,

get_vehtype EXPORTING ex_vehtype TYPE string

ex_n_o_veh TYPE i,

  • Begin of IC240107

  • To change the implementation of a superclass instance method in *

  • subclass, without changing the method signature, the method must be *

  • defined in subclass with REDIFINITION addition. There is no need to *

  • define the method parameters and exceptions again. *

  • Redifinition is not possible for private methods. *

  • Redefining methods introduces POLYMORPHISM in classes. *

  • REDEFINITION addition must be used to implement an abstract method *

  • of a superclass in a subclass. However a final method in a *

  • superclass can't be redefined in superclass. *

  • The constructors of a class are always final and cannot be redefined.*

get_make REDEFINITION.

  • End of IC240107

PRIVATE SECTION.

DATA gv_vehtype TYPE string.

ENDCLASS. "zl_lcl_car DEFINITION

&----


*& Class (Implementation) zl_lcl_car

&----


  • Implementation of the Subclass

----


CLASS zl_lcl_car IMPLEMENTATION.

METHOD set_vehtype.

IF im_vehtype IS NOT INITIAL.

gv_vehtype = im_vehtype.

  • Protected component of superclass can be directly accessed in subclass. *

  • The same applies for public components, private compenets of superclass *

  • have to be accessed using public or protected methods of superclass. *

gv_n_o_veh = im_n_o_veh.

ENDIF.

ENDMETHOD. "set_vehtype

METHOD get_vehtype.

ex_vehtype = gv_vehtype.

ex_n_o_veh = gv_n_o_veh.

ENDMETHOD. "get_vehtype

  • Begin of IC240107

  • When REDIFINITION addition is used with any method in subclass *

  • definition a new implementation part must be specified for the *

  • inherited method. *

METHOD get_make.

  • To access the superclass method from the redefined method, use the *

  • pseudo-reference SUPER *

super->get_make( IMPORTING ex_make = ex_make

ex_model = ex_model ).

ENDMETHOD. "get_make

  • End of IC240107

ENDCLASS. "zl_lcl_car

  • Declaring a reference variable with reference to the subclass.

DATA: z_car TYPE REF TO zl_lcl_car.

DATA: gv_make TYPE string,

gv_model TYPE string,

gv_vehtype TYPE string,

gv_n_o_veh TYPE i.

START-OF-SELECTION.

  • Creating an instance of the subclass

CREATE OBJECT: z_car.

  • Accessing the public methods of superclass using subclass reference

z_car->set_make( EXPORTING im_make = 'MARUTI'

im_model = '800' ).

  • POLYMORPHISM - There are 2 implementations of the method get_make *

  • one in superclass and one in subclass. Since the method is *

  • redefined in subclass, subclass implementation will be executed *

  • if subclass reference is used. *

z_car->get_make( IMPORTING ex_make = gv_make

ex_model = gv_model ).

  • Accessing the methods of subclass using subclass reference

z_car->set_vehtype( EXPORTING im_vehtype = 'Car'

im_n_o_veh = '10' ).

z_car->get_vehtype( IMPORTING ex_vehtype = gv_vehtype

ex_n_o_veh = gv_n_o_veh ).

WRITE: / 'Vehicle type :', gv_vehtype,

/ 'Make :', gv_make,

/ 'Model :', gv_model,

/ 'No of ordered vehicles :', gv_n_o_veh.

<u><b>Interfaces & Polymorphism</b></u>

Interfaces cannot be instantiated, hence an interface reference can only refer

to the instance of classes that have implemented the interface.

To perform polymorphism with interfaces, narrowing cast must be used to

copy an object reference of the class to a reference variable typed to the

interface. The interface reference can then be used to access only the

interface components in a class and not the specific class components.

Using the interface reference the interface components can be directly

accessed. No prefixing is required.

Interface references (obtained after narrowing cast) can be used to call

methods with the same name, whereby the different implementations can be

executed depending on the class whose object reference is assigned to the

interface reference.

<u><b>SOURCE CODE</b></u>

&----


*& Interface (Definiton) zl_lif_vehicle

&----


  • Definition of Vehicle Interface.

  • This interface will be used for car and bike class.

  • Interfaces contain no implementation.

----


INTERFACE zl_lif_vehicle.

*No visibility levels in interface. All components are by default public

METHODS: set_make IMPORTING value(im_make) TYPE string

im_model TYPE string,

get_make EXPORTING value(ex_make) TYPE string

ex_model TYPE string

ex_type TYPE string. " IC010107+

ENDINTERFACE. "zl_lif_vehicle

&----


*& Class (Definiton) zl_lcl_car

&----


  • Definition of Car class

----


CLASS zl_lcl_car DEFINITION.

PUBLIC SECTION.

  • To specify the interface name in the public section of the class as

  • interfaces can only be implemented publicly

INTERFACES zl_lif_vehicle.

  • Defining an additional method specific to the car class

METHODS check_availability. " IC010107+

PRIVATE SECTION.

DATA: gv_veh_type TYPE string, " Vehicle type " IC010107+

gv_make TYPE string, " Vehicle make

gv_model TYPE string. " Type or model

ENDCLASS. "zl_lcl_car DEFINITION

&----


*& Class (Definiton) zl_lcl_bike

&----


  • Definition of Bike class

----


CLASS zl_lcl_bike DEFINITION.

PUBLIC SECTION.

  • To specify the interface name in the public section of the class as

  • interfaces can only be implemented publicly

INTERFACES zl_lif_vehicle.

  • To simplify accessing interface components, aliases are used

ALIASES: bike_set_make FOR zl_lif_vehicle~set_make,

bike_get_make FOR zl_lif_vehicle~get_make.

PRIVATE SECTION.

DATA: gv_veh_type TYPE string, " Vehicle type " IC010107+

gv_make TYPE string, " Vehicle make

gv_model TYPE string. " Type or model

ENDCLASS. "zl_lcl_bike DEFINITION

&----


*& Class (Implementation) zl_lcl_car

&----


  • Implementation of Car class

----


CLASS zl_lcl_car IMPLEMENTATION.

  • Implementing the interface components in the class. The interface

*components are distinguished from other components in implementing

*class

  • by prefixing interface name followed by a tilde ~

METHOD zl_lif_vehicle~set_make.

IF im_make IS NOT INITIAL

AND im_model IS NOT INITIAL.

gv_veh_type = 'car'. " IC010107+

gv_make = im_make.

gv_model = im_model.

ENDIF.

ENDMETHOD. "set_make

METHOD zl_lif_vehicle~get_make.

ex_type = gv_veh_type. " IC010107+

ex_make = gv_make.

ex_model = gv_model.

ENDMETHOD. "get_make

  • Begin of IC010107

  • Implementing the method specific to the car class

METHOD check_availability.

WRITE / 'Available'.

ENDMETHOD. "check_availability

  • End of IC010107

ENDCLASS. "zl_lcl_car IMPLEMENTATION

&----


*& Class (Implementation) zl_lcl_bike

&----


  • Implementation of Bike class

----


CLASS zl_lcl_bike IMPLEMENTATION.

METHOD zl_lif_vehicle~set_make.

  • Implementation of this method in the bike class is different from

  • that in car class. Thus any method defined in the interface can be

*implemented differently in different classes implementing the

*interface.

IF im_make IS NOT INITIAL

AND im_model IS NOT INITIAL.

gv_veh_type = 'bike'. " IC010107+

gv_make = im_make.

gv_model = im_model.

ENDIF.

ENDMETHOD. "set_make

METHOD zl_lif_vehicle~get_make.

ex_type = gv_veh_type. " IC010107+

ex_make = gv_make.

ex_model = gv_model.

ENDMETHOD. "get_make

ENDCLASS. "zl_lcl_bike IMPLEMENTATION

*Declaring reference variable with reference to the car & bike class and

  • a reference variable typed to the interface

DATA: z_car TYPE REF TO zl_lcl_car,

z_bike TYPE REF TO zl_lcl_bike,

z_partner TYPE REF TO zl_lif_vehicle. " IC010107+

DATA : gv_make TYPE string,

gv_model TYPE string,

gv_veh_type TYPE string. " IC010107+

START-OF-SELECTION.

  • Create instance of the class

CREATE OBJECT: z_car,

z_bike.

*&----


-------

  • To perfrom polymorphism with interfaces copy the object reference

  • of the car class to a reference variable typed to the interface.

z_partner = z_car. " IC010107+

*&----


**

  • Accessing the interface components by using an object reference

  • whose class implements the interface.

  • z_car->zl_lif_vehicle~set_make( EXPORTING im_make = 'HYUNDAI'

  • im_model = 'SANTRO' ).

  • z_car->zl_lif_vehicle~get_make( IMPORTING ex_make = gv_make

  • ex_model = gv_model ).

*&----


**

  • The instance of the implementing class can now be addressed

  • using the interface. The prefixing of interface name and ~ operator

  • can be omitted. However only the interface componenets and not the

*class specific componets (e.g. method check_availability) can be

*addressed

*using reference variable z_partner. So this type of assignment of

*reference

  • variables is called NARROWING CAST.

z_partner->set_make( EXPORTING im_make = 'HYUNDAI'

im_model = 'SANTRO' ). " IC010107+

z_partner->get_make( IMPORTING ex_make = gv_make

ex_model = gv_model

ex_type = gv_veh_type ). " IC010107+

z_car->check_availability( ).

WRITE: / 'Make :', gv_make,

/ 'Model :', gv_model,

/ 'Vehicle type :', gv_veh_type. " IC010107+

*&----


-------

  • Begin of IC010107

  • Object reference variable of the bike class copied to the

  • Interface reference variable

z_partner = z_bike.

*Implementation that is executed when methods are called depends on the

*object

*to which interface reference variable currently refers (bike in this

*case)

z_partner->set_make( EXPORTING im_make = 'SUZUKI'

im_model = 'HAYABUSA' ).

z_partner->get_make( IMPORTING ex_make = gv_make

ex_model = gv_model

ex_type = gv_veh_type ).

z_car->check_availability( ).

WRITE: / 'Make :', gv_make,

/ 'Model :', gv_model,

/ 'Vehicle type :', gv_veh_type.

  • End of IC010107

Award points if found useful.

Regards

Indrajit

Read only

Former Member