This is probably a common Objective-C question reported by Java coders, but I don’t know what to call it or how to search for the answer. Let’s say I have a class and another class which extends it:
AbstractModel
@interface AbstractModel {
}
ModelImpl
@interface ModelImpl : AbstractModel {
}
Separate from these, I have two more classes, again one extending the other:
ControllerA
@interface ControllerA {
AbstractModel *foo;
}
@property (nonatomic, retain) AbstractModel *foo;
ControllerB
@interface ControllerB : ControllerA {
}
I want to be able to say that foo in ControllerA can contain an AbstractModel or any of its subtypes. However, the compiler gives me a warning if I attempt to store anything other than an AbstractModel in it. (Of course I understand that classes can’t really be abstract in ObjC, but have mercy on me.)
I would also like to be able to “lock down” the foo property in specific subclasses. I would like to say that foo in ControllerB can contain only a ModelImpl4 for example. Is this possible?
What is the conventional Objective-C best practice for solving this type of problem? Is using inheritance in this way — or to achieve this goal — just not a good idea in Objective-C?
First, I want to understand this:
This doesn’t make sense. You should be able to assign sub-classes of
AbstractModeltofoowithout trouble. What problem are you seeing?Next, what you’re describing is not overriding, it’s overloading. You’re trying to change the return type of the method, and you cannot do that in ObjC. There are very good solutions to this problem, but it somewhat depends on what your real goal is.
First, you can get rid of
-fooinControllerA. IfControllerAis actually abstract, then it perhaps is better not to have one. IfControllerAis abstract, I definitely recommend that you get rid of thefooivar at that layer. You should put the ivars in the subclasses.Alternately, you can add typed methods to the subclasses. For instance,
ControllerBwould have a-modelBFoomethod in addition to-foothat it inherits. These methods would be identical; they would just have different return types, allowing good typing in all callers.Do not ignore warnings. They’re there to protect you (and in ObjC, they’re about all you have to protect you). Limit your typecasting as much as you can. They move compiler errors (good) to run-time exceptions (bad).