Objective-C inheritance; calling overriden method from superclass?

Posted by anshuchimala on Stack Overflow See other posts from Stack Overflow or by anshuchimala
Published on 2010-12-25T21:29:28Z Indexed on 2010/12/26 12:54 UTC
Read the original article Hit count: 154

Filed under:
|
|

Hello,

I have an Objective-C class that has a method that is meant to be overridden, which is uses in a different method. Something like this:

@interface BaseClass
- (id)overrideMe;
- (void)doAwesomeThings;
@end

@implementation BaseClass
- (id)overrideMe {
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}
- (void)doAwesomeThings {
    id stuff = [self overrideMe];
    /* do stuff */
}
@end

@interface SubClass : BaseClass
@end

@implementation SubClass
- (id)overrideMe {
    /* Actually do things */
    return <something>;
}
@end

However, when I create a SubClass and try to use it, it still calls overrideMe on the BaseClass and crashes due to doesNotRecognizeSelector:. (I'm not doing a [super overrideMe] or anything stupid like that).

Is there a way to get BaseClass to call the overridden overrideMe?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about oop