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?