I have 2 sub classes; SubA and SubB that inherit from Base.
The sub classes have the same method: SubMethodA. This method is written the exact same way for both subclasses. It's differences are in it's execution. Within SubMethodA, it calls SubMethodB which executes differently depending on the subclass.
I figured I could move SubMethodA to Base but of course Base doesn't know about SubMethodB.
• First Try: I added the prototype for SubMethodA and it's implementation to Base class. I then added to the interface of Base the prototype for SubMethodB. In the implementation for Base, I implemented SubMethodB as an empty stub. This would allow for a "pass thru" to the subclass; the subclass would override SubMethodB. This worked but something felt wrong about it. Having an empty stub just to get this to work doesn't feel correct. Not sure why but my inner programmer was not happy.
• Second Try: I left the SubMethodB prototype in the Base header. I removed the empty stub and of course I got an, "Incomplete Implementation" warning. Compiles and runs correctly but is not correct.
• Third Try: I removed the prototype from the Base header. The last warning was gone but I got a new one. "Instance method '-SubMethodB:' not found (return type defaults to 'id')". Again the warning makes sense and the code runs properly but this is not correct.
• Fourth Try: I made a @protocol called "BaseProtocol" which is implemented by both subclasses; SubA and SubB. I still have the same warning from the last try.
• Fifth Try: In a last ditch effort, I surrounded the offending code with "[self conformsToProtocol:@protocol(BaseProtocol)]" to see if the compiler would notice that I am checking to make sure that this method is really implemented by the subclass and not give me a warning but it didn't make difference. Same warning as last time.
So here are my questions:
- Is this idea of a base class calling a subclass's method a pipe dream or is there a way that this can be done properly without any warnings?
- Is my thought pattern incorrect in wanting to have the exact same code that is in 2 subclasses, in the base class?
Any help, insight or suggestions would be greatly appreciated.