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

Dynamic creation of subclass object using superclass

Former Member
0 Likes
3,469

Hi,

I am designing an application in which I want to create a Superclass which contains attributes which are generic. Now I want to create subclasses to this superclass which should contain one method (same method name) but different operations in each method of subclasses. I want to use the Open and Close principle of ABAP Objects in my application.

I am hoping that I can create an object of the Superclass in my application and set the attributes of the superclass first. Then I call a method of the superclass passing a variable(which helps in determining the subclass dynamically) and inside the method dynamically create a object of the subclass and call the method of subclass. But the subclass method should be able to access the attributes of superclass to do the calculation.

This is a simple example of the ABAP object design which i am trying, hope this will make it easier to understand the design.

Super class: LCL_CAR.

Attributes: tyre_size.

method: change_tyre

               importing i_model.

Super class: LCL_CAR implementation.

method: change_tyre.

data: v_subclass type string.

concatenate 'LCL_' i_model into v_subclass.

create lo_sub type (v_subclass)  (dynamically creating the subclass object)

lo_sub->change_tyre. (this should call the relevant subclass FORD/BMW)

endmethod

Subclass: LCL_FORD inheriting LCL_CAR.

method: change_tyre.

Subclass: LCL_BMW inheriting LCL_BMW.

method: change_tyre.

In my application,

Data: lo_car_obj type ref to LCL_CAR.

create object lo_car_obj.

lo_car_obj->tyre_size = 17.  (here i have set the attributes of the superclass)

lo_car_obj->change_tyre ( i_model = 'FORD' ).  (intended to change the Tyre of FORD)

lo_car_obj->change_tyre( i_model = 'BMW' ).     (intended to change the Tyre of BMW)

I am not sure if this is the best design to do it or not. I just want to know what can i do to dynamically create the subclass and calls the method of subclass which could access the attributes of superclass ?

Hope I have explained what I am looking for.

Hi,

I am designing an application in which I want to create a Superclass which contains attributes which are generic. Now I want to create subclasses to this superclass which should contain one method (same method name) but different operations in each method of subclasses. I want to use the Open and Close principle of ABAP Objects in my application.

I am hoping that I can create an object of the Superclass in my application and set the attributes of the superclass first. Then I call a method of the superclass passing a variable(which helps in determining the subclass dynamically) and inside the method dynamically create a object of the subclass and call the method of subclass. But the subclass method should be able to access the attributes of superclass to do the calculation.

This is a simple example of the ABAP object design which i am trying, hope this will make it easier to understand the design.

Super class: LCL_CAR.

Attributes: tyre_size.

method: change_tyre

               importing i_model.

Super class: LCL_CAR implementation.

method: change_tyre.

data: v_subclass type string.

concatenate 'LCL_' i_model into v_subclass.

create lo_sub type (v_subclass)  (dynamically creating the subclass object)

lo_sub->change_tyre. (this should call the relevant subclass FORD/BMW)

endmethod

Subclass: LCL_FORD inheriting LCL_CAR.

method: change_tyre.

Subclass: LCL_BMW inheriting LCL_BMW.

method: change_tyre.

In my application,

Data: lo_car_obj type ref to LCL_CAR.

create object lo_car_obj.

lo_car_obj->tyre_size = 17.  (here i have set the attributes of the superclass)

lo_car_obj->change_tyre ( i_model = 'FORD' ).  (intended to change the Tyre of FORD)

lo_car_obj->change_tyre( i_model = 'BMW' ).     (intended to change the Tyre of BMW)

I am not sure if this is the best design to do it or not. I just want to know what can i do to dynamically create the subclass and calls the method of subclass which could access the attributes of superclass ?

Hope I have explained what I am looking for.

6 REPLIES 6
Read only

matt
Active Contributor
2,450

Check out the factory pattern.

In the superclass, you need a static method (e.g. factory or get_instance) that returns an instance (e.g. r_car) of LCL_CAR. It should take parameter i_model. It should contain code like:

data: v_subclass type string.

concatenate 'LCL_' i_model into v_subclass.

create r_car type (v_subclass)

Call the static method to get an instance of your superclass.


data car type ref to lcl_car.

car = lcl_car=>factory( 'Ford' ).

car->change_tyre( ).

other_car = lcl_car=>factory( 'VW' ).

car->change_emissions_software( ).

etc.

Read only

0 Likes
2,450


other_car = lcl_car=>factory( 'VW' ).

car->change_emissions_software( ).

i think this wouldn't work, because change_emission_software is obviously declared in lcl_vw and not in lcl_car (and car is of type lcl_car). Otherwise this would mean that every car manufacturer could manipulate their emissions software, and we all know they can't do that

Read only

matt
Active Contributor
0 Likes
2,450

No no. They all need a change_emissions_software method. It's just that the VW subclass override of this method contains

if me->is_in_testmode( ) eq abap_true.

me->hobble_engine_for_low_emissions( ).

endif.

Read only

Former Member
0 Likes
2,450

Hi Matthew,

Thanks for the reply.

If I go with this approach, then I am creating separate instance for each sub-classes. And these subclasses doesn't share the values in the superclass attributes.

I believe, then I have to make all the attributes of the superclass as static so that the values in it is shared among subclasses. But I am hesitant to do that.

Is my understanding correct ?

Regards,

Bestin

Read only

matt
Active Contributor
0 Likes
2,450

I think you misunderstand object orientated design and how inheritance works.  If one car is a VW and one car is a Ford, you need two instances.  Although they have attributes with the same name (defined at car level) they have their own set of values.

In the same way, in the real world, all cars have an horsepower rating, but the actual value of the hp is an attribute of each car. Horsepower would be defined at the lcl_car level as an instance attribute.

I suggest you go through some basic tutorials on object oriented design. These concepts are well covered in many many places - they're not even abap specific.

Read only

Former Member
0 Likes
2,450

>  other_car = lcl_car=>factory( 'VW' ).

>  car->change_emissions_software( ).


))))))

wittily, Matthew!