04-17-2013 11:22 PM
Hi,
I have 2 class: cl_crm_mktpl_cpg_fusg_coll and zcl_crm_mktpl_cpg_fusg_coll.
Class zcl_crm_mktpl_cpg_fusg_coll inherit from cl_crm_mktpl_cpg_fusg_coll.
I wannt to do Widening Cast, but i get casting exception.
DATA: lr_fuh_coll TYPE REF TO cl_crm_mktpl_cpg_fusg_coll, "Fund Usage Collection,
lr_zfuh_coll TYPE REF TO zcl_crm_mktpl_cpg_fusg_coll. "Fund Usage Collection
lr_fuh_coll ?= ir_mktproject->get_assignment_collection( cl_crm_mktpl_constants=>asg_type_fusg ).
lr_zfuh_coll ?= lr_fuh_coll. <---- Casting Exception Runtime.
Some idea?
04-18-2013 8:35 AM
Hi Erick,
You can use this casting only in case if method below returns instance of ZCL_CRM_MKTPL_CPG_FUSG_COLL:
lr_fuh_coll ?= ir_mktproject->get_assignment_collection( cl_crm_mktpl_constants=>asg_type_fusg ).
I guess this is not the case in your example that is why you have casting exception. Maybe method get_assignment_collection returns instance of CL_CRM_MKTPL_CPG_FUSG_COLL?.
Regards
Adam
04-18-2013 8:35 AM
Hi Erick,
You can use this casting only in case if method below returns instance of ZCL_CRM_MKTPL_CPG_FUSG_COLL:
lr_fuh_coll ?= ir_mktproject->get_assignment_collection( cl_crm_mktpl_constants=>asg_type_fusg ).
I guess this is not the case in your example that is why you have casting exception. Maybe method get_assignment_collection returns instance of CL_CRM_MKTPL_CPG_FUSG_COLL?.
Regards
Adam
04-18-2013 2:23 PM
Hi Adam.
Method get_assignment_collection returns instance of CL_CRM_MKTGS_ASG_COLLECTION
The class hierarchy is:
ZCL_CRM_MKTPL_CPG_FUSG_COLL inherits from CL_CRM_MKTPL_CPG_FUSG_COLL
CL_CRM_MKTPL_CPG_FUSG_COLL inherits from CL_CRM_MKTPL_FUSG_COLL
CL_CRM_MKTPL_FUSG_COLL inherits from CL_CRM_MKTPL_ASG_RT_COLLECTION
CL_CRM_MKTPL_ASG_RT_COLLECTION inherits from CL_CRM_MKTPL_ASG_COLLECTION <--result of get_assignment_collection
I did a copy of CL_CRM_MKTPL_CPG_FUSG_COLL and i get same error:
lr_zfuh_coll ?= ir_mktproject->get_assignment_collection( cl_crm_mktpl_constants=>asg_type_fusg ).
Regards
Ty Adam.
04-18-2013 6:20 PM
Hi Erick,
That explains why you cannot do casting and error is thrown. You try to cast parent, general class to specified child and that is not allowed. The parent class does not now about subclass extended details (attributes and methods).
I will try to explain in simple example.
Your CL_CRM_MKTGS_ASG_COLLECTION class is basic pizza with cheese, lets call it CL_CHEESE_PIZZA.
Your ZCL_CRM_MKTPL_CPG_FUSG_COLL is still basic pizza with cheese (extends CL_CRM_MKTGS_ASG_COLLECTION), but has additions like pepperoni and paprika - lets call it ZCL_PEPPERONI_PIZZA.
You can do:
CL_CHEESE_PIZZA ?= ZCL_PEPPERONI_PIZZA - pepperoni is still pizza with cheese.
But you cannot do:
ZCL_PEPPERONI_PIZZA ?= CL_CHEESE_PIZZA - if you have basic pizza with cheese you cannot say that it is pepperoni.
That is why you cannot cast:
ZCL_CRM_MKTPL_CPG_FUSG_COLL ?= CL_CRM_MKTGS_ASG_COLLECTION.
Of course if your method get_assignment_collection returned instance of ZCL_CRM_MKTPL_CPG_FUSG_COLL, then even if its return type is CL_CRM_MKTGS_ASG_COLLECTION in method definition, your casting would work. It is like method returns CL_CHEESE_PIZZA, but you can pass actually parameter ZCL_PEPPERONI and it will still work.
I hope it helps. I tried first with VEHICLE and PLANE but then thought that example with pizza will be simpler.
Regards
Adam
04-18-2013 6:35 PM
Hi Adam,
Nice example, now I understand the cause of my problem.
I'll have to read again Wedening and Narrow Casts concepts, because I misunderstood the terms
My requirement is add new functionality with out replace standard methods, because we dont want affect standard programs.
I did a Enhancement Class to add Z methods in CL_CRM_MKTPL_CPG_FUSG_COLL and can solve some points, It looks good.
Regards