Suppose I have a class called 'Node' that has a method 'addChild'
@interface Node : NSObject
-(void) addChild:(Node *)n;
and I have a subclass called 'Sprite' that shouldn't respond to this method.
@interface Sprite : Node
I know I could do something like this:
@implementation Sprite
-(void) addChild:(Node *)n {
NSLog(@"Don't call addChild on a Sprite!");
}
or
-(void) addChild:(Node *)n {
NSAssert(NO, @"Don't call addChild on a Sprite!");
}
But is there a way to declare that the subclass doesn't respond to this method, and have the compiler throw an error? Getting a compiler error would be a lot better than a runtime error in this case.
EDIT
I realize this violates the Liskov substitution principle. But in Apple's documentation there's this:
"Any doesNotRecognizeSelector: messages are generally sent only by the runtime system. However, they can be used in program code to prevent a method from being inherited. For example, an NSObject subclass might renounce the copy or init method by re-implementing it to include a doesNotRecognizeSelector: message..."
I don't understand why throwing a runtime error rather than a compile-time error would be any less in violation of this principle.