Fables Alive
Fables Alive

Reputation: 2810

Calling submethod from Parent in ObjectiveC (IOS6)

SquareGeo, TriangleGeo is drived from Geo class.
Each of SquareGeo, TriangleGeo classes have drawMe methods with same parameters but different content.

I wantto call drawMe from the base class is Geo but, issue Navigator says me a warning:

"Instance method '-drawMe' not found (return type defaults to 'id')"

in conclusion:
How can I call a method from Parent that is only defined all its child Classes.

shall I avoid warning ? should I use protocols ? looking something like this:

[sub drawMe]; //not super

any idea ? (please do not refer me to java examples.only objectC knowhow)

Upvotes: 0

Views: 138

Answers (2)

Paul.s
Paul.s

Reputation: 38728

The easiest way to overcome this is to provide an empty implementation in the parent class.

If you want to ensure that subclasses do provide their own implementation then you can add an exception to the base class' method

@implementation Base

- (void)drawMe;
{
  [NSException raise:NSInternalInconsistencyException 
        format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
}

Upvotes: 0

Dan F
Dan F

Reputation: 17732

Thats the beautiful thing about inheritance, you declare the method in the parent, and implement it in the children, and the underlying system handles calling the "correct" version of the method depending on what type of class the instance actually is.

Upvotes: 1

Related Questions