Mimicking Abstract Classes with Objective-C

by Hugh
July 03, 2010

Objective-C doesn’t have the abstract class construct. The common approach to mimic it is to use NSObject’s doesNotRecognizeSelector:

@interface ShapeBase : NSObject {}
- (void)draw {
    [self doesNotRecognizeSelector:_cmd];

@interface Circle : ShapeBase {}
// missing draw method

This forces subclasses to override, otherwise you get a runtime exception. There’s also another approach we can take using a formal protocol.

@protocol ShapeBehavior
- (void)draw;

@interface ShapeBase : NSObject {}

@interface Circle : ShapeBase<ShapeBehavior> {}
@end // Compile error, missing draw method

Let’s also add some sugar so we don’t have to type ShapeBehavior every time we want to use a shape:

typedef ShapeBase<ShapeBehavior> Shape;

// Before typedef
ShapeBase<ShapeBehavior> *circle = [[Circle alloc] init];
[circle draw];

// After typedef
Shape *circle = [[Circle alloc] init];
[circle draw];

Using formal protocols gives you errors earlier in the development process but the trade-off is that it’s not as lightweight as simply using doesNotRecognizeSelector.