Objective-C multiple inheritance

Objective-C doesn’t support multiple inheritance, and you don’t need it. Use composition:

@interface ClassA : NSObject {
}

-(void)methodA;

@end

@interface ClassB : NSObject {
}

-(void)methodB;

@end

@interface MyClass : NSObject {
  ClassA *a;
  ClassB *b;
}

-(id)initWithA:(ClassA *)anA b:(ClassB *)aB;

-(void)methodA;
-(void)methodB;

@end

Now you just need to invoke the method on the relevant ivar. It’s more code, but there just isn’t multiple inheritance as a language feature in objective-C.

Leave a Comment