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

calling a method inside a subclass - factory pattern

former_member182337
Participant
0 Likes
1,705

Hello,

I am working on creation of a factory pattern. The "factory" method inside the class will instantiate the sub classes based on the importing parameter. The subclasses redefine an abstract method in the super class and the same is being called in my program. It is fine till this stage. The problem that I am facing is that am not able to implement a separate method for the subclass. When I try to call the method ( Exclusive for the sub class not inherited from the super class) , I get the error message - "The method is unknown, protected or private" . There is no syntax error while I try to access a message inherited from the super class. Can you please share some thoughts?

Thank you.

Regards,

Prem

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,312

Post your code snippet so that others can have a better understanding of your problem.

If i understand correctly you're trying to call the method of the subclass using the reference variable whose static type is that of the superclass


cl_factory=>get_instance(...)->sub_class_method(...)

6 REPLIES 6
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,313

Post your code snippet so that others can have a better understanding of your problem.

If i understand correctly you're trying to call the method of the subclass using the reference variable whose static type is that of the superclass


cl_factory=>get_instance(...)->sub_class_method(...)

Read only

0 Likes
1,312

Hello Suhas,

Thanks for your reply. Please find the code snippet below.

go_val = ycl_factory=>factory( exporting iv_type = 'SO').

if go_val is bound.

go_val->calculate( exporting iv_customers = ip_sales

                              receiving ep_customers = gt_sales ) .

go_val->calculate_sub ().

endif.

endif.


The above code is fine till the calculate method which is inherited from the super class . The calculate_sub is a method that is available only in the child class which is instantiated by go_val.
and that is giving a syntax error.

Read only

0 Likes
1,312

Hi Prem,

you first need to cast your go_val to the type of the subclass. Something like this:

data: sub_class    type ref to cl_sub_class.

sub_class ?= ycl_factory=>factory( ...).

sub_class->calculate_sub( ).


However, this code looks like you are having a design issue. Usually it should not be necessary to call a specific method of the subclass. Instead you should stick with the interface provided by your factory.


Christian

Read only

0 Likes
1,312

Thanks Christian.

Can you give some example working with the interface ?

Read only

0 Likes
1,312

Hi Christian,

Any examples please?

Read only

0 Likes
1,312

Hi Prem,

it's hard to give an example as you question is quite abstract. However, I'd try to implement you framework in such a way that you don't need to know the specific subclass to invoke the required method. This can easily be achieved using an interface:


data: my_example type ref to zif_example_interface.

my_example = zcl_factory=>get_implementation( ...).

my_example->execute_if_method( ).

You define a interface (zif_example_interface) that has the necessary methods (execute_if_method). Your subclasses implement this interface and the specific functionality in the concrete interface implementation.

The factory now return a reference to the interface (the concrete implementation depends on the parameters). You now simply invoke the interface methods and don't need care about the concrete implementation returned by the factory.

Hope this helps.

Christian