I am trying to create a Factory that should return another common interface object (say Item ) according to the input parameter (I call it context) of the getItem(A context) function
Now suppose I define a new type of context: B , which inherits from A
I wanted to return another element depending on whether the object passed in Factory passed class B or A
I tried to do the following (method overload):
class Factory { static Item getItem(A context) {...} static Item getItem(B context) {...} }
This works fine if I do something like this:
B bContext=new B(); Item it=Factory.getItem(bContext);
However, if I create and create an object of type A :
A bContext=(A) new B(); Item it=Factory.getItem(bContext);
The first Factory method is called.
I thought that polymorphism would ensure the second method was executed even after the throw, and I would like to know if I missed something?
I know that I can continue to use one method and use the is operator to check what a variable type is, but I thought the solution presented above was a little more elegant.
Srkx
source share