Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

whitehexagon

macrumors regular
Original poster
May 12, 2007
147
0
I have a base class with several child classes. I want to declare a method in the base class that all the extending classes are forced to implement. ie the base class itself has no implementation.

Is this possible with objective-c, so far I'm only finding references to overiding the implementation from the base class which is not the same thing.
 
Thanks! that looks almost like what I need.

So I created a protocol definition in the header for the base class. My base class has the <MyProtocol> but this is forcing me to implement the protocol on the base class, but not the children. The reverse of what I wanted. What am I doing wrong, or is this the way protocol works?
 
I think you'd have to make the child objects conform to the protocol as well to force compile-time implementation of your method. Or just do some run-time stuff, like in the base class method throw an exception so that you can catch which child classes haven't implemented it.
 
Then is the protocol adding anything useful in this case? Sounds the same as just having a normal exception throwing method in the base class, and relying on the child classes to overide the method.

Is there some other way to implement an abstract method in the base class?
 
Is there some other way to implement an abstract method in the base class?

I don't think so. I've never seen a design where a protocol was used to call a derived class. It's normally used as part of implementing the delegate pattern.

Obj-C just doesn't have the same thing as a pure virtual method in C++.
 
Thanks, I can stop hunting around then. I'm sure someone could make a lot of money with a book ' Objective-c for Java programmers' :)
 
Thanks, I can stop hunting around then. I'm sure someone could make a lot of money with a book ' Objective-c for Java programmers' :)

I wish there was a reference table that listed various concept names for different programming languages. Concepts like: abstract virtual methods in C++, interfaces in Java, Protocols in Obj-C, Categories in Obj-C, constructors in C++, etc and how they compare to each other.
 
Actually wikipedia is not the worst source for such comparisons, it's just too brief.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.