Implement a pure virtual method in Objective-C

When you program in Objective-C you need to purge your mind of such things as virtual methods. You don’t call methods on Objective-C objects, you send messages to them. Objects either respond to messages or they don’t, but due to the dynamic binding, you can’t tell this until run time.

Thus, you can declare a method on a base object and not not provide an implementation, no problem (except for the compiler warning), but you can’t have the compiler flag up when you directly instantiate an object with such methods and it won’t throw an error at runtime unless you actually send that message to the object.

The best way to create “virtual” base classes (in my opinion) is to declare the method and give it a stub implementation that throws a suitable exception.

Leave a Comment