I have a class which is supposed to be abstract. In one of it's abstract methods the return type may be an instance of class1,class2 or class3, depending on the class that's implementing the method. I'm wondering how should I declare the method in the abstract class. I thought about using dynamic typing, but I want the return type to be restricted to one of the 3 classes, not every type, and in addition I'm not sure I can override it so that in the inheriting class the return type will not match the return type in the abstract class.
I'd be glad if you could help me with this,
Tnx!
Take a look at this:
This is rather condensed syntax, and memory management sucks, but you can see that
newItem
is virtual (sendingnewItem
tomyBA
returns aB
) and covariant, which seems to be what you want.Note that you could also do:
but that returns an
A
, and sendingfoo
to it would tell you that the class does not respond to selector#foo
. If you omitted the(B*)
cast, you would get a warning about this at compile time.But ISTM that covariance is no big problem, in Objective-C.