2
votes

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.

2
FWIW, what I've seen most often in a variety of languages (and therefore have got comfortable with) is the stub approach. For clarity, the "stub" often has an implementation that throws an error tagged with something that says, "subclass responsibility". - Phillip Mills

2 Answers

0
votes

Concerning the warning:

You can have optional methods in your protocols. That will remove the warning of an incomplete implementation.

About the design:

The design that you are trying to do differs from common design patterns in Objective-C, inheritance is rather unusual. Instead delegation is often used to customize implementations. If the methods are as similar as you say then maybe you can find a place for delegateA and delegateB to change what will happen. Maybe your "base" class (within quotes since its no longer having any subclasses) could have an API where delegateA and delegateB can send blocks of code to execute at different points within methodA

0
votes

I would put SubMethodA in the Base class. Then in each subclass, override the SubmethodA. In the override, you would call [super SubMethodA] and then add your SubmethodB in whichever way you like.