I need to do a check in class A, regardless of whether I myself am ClassB or ClassC.
The best way to do this is to call an abstract method that can be defined in your subclasses:
ClassA:
- (void)doThing { [self doSpecializedThing]; } - (void)doSpecializedThing { return; }
ClassB:
- (void)doSpecializedThing { // ClassB specialized version of whatever ClassA needs to do }
ClassC:
- (void)doSpecializedThing { // ClassC specialized version of whatever ClassA needs to do }
This prevents ClassA from knowing anything specific about its subclasses, as it is almost always a bad idea.
You can also override -doThing in ClassB and ClassC and call them [super doThing] in your implementation. This is not necessarily the right decision in every case, although, for example, when the code in ClassA -doThing relies on some behavior in subclasses (for example, if -doSpecializedThing should return the value used in -doThing ).
Caleb source share