todd412
todd412

Reputation: 1318

How can I have a custom class inherit from multiple superclasses?

I have a an iPhone app in which class A is a subclass of UIViewController:

enter image description here

Now I'm creating a new app, in which I want to re-use A, but have it subclass from B, which is fine, because B subclasses from UIViewController:

enter image description here

But now I have another class, C, which is a subclass of GLKViewController:

enter image description here

The problem comes when I try to make a third app which re-uses B without any changes. Since B inherits from UIViewController, I need to somehow tell C that it should inherit from B and from GLKViewController, which I believe is a case of multiple inheritance:

enter image description here

From what I'm reading, the best way to handle this is with composition, but I don't understand how to best apply it to my situation.

As a stand-in solution, I realized I could just create a wrapper class D which I can then modify on an app-by-app basis to subclass from the appropriate superclass for the task at hand:

enter image description here

enter image description here

But that seems kind of hacky. Is there a better way to do this?

Upvotes: 9

Views: 4739

Answers (3)

Till
Till

Reputation: 27597

You could possibly get that covered by using a category. It certainly is not the same as multiple inheritance but sometimes does the job.

Let me draft an example for a category and see if that does what you aim for.

Header: UIViewController+MyAwesomeExtension.h

@interface UIViewController (MyAwesomeExtension)
- (void)doSomething;
@end

Implementation: UIViewController+MyAwesomeExtension.m

@implementation UIViewController (MyAwesomeExtension)
- (void)doSomething
{
    NSLog(@"doing something totally awesome");
}
@end

Now you are using it from within one of your UIViewController subclasses which also includes like e.g. GLKViewController ...

Implementation:

#import "UIViewController+MyAwesomeExtension.h"
...
[self.viewControllerDerivedClass doSomething];
...

Note that such category does have its limits. To find out more, how about researching the subject a bit further.


Overall, I think the "most" correct answer would be using protocols to get as close to multiple inheritance as possible, as Mike C. drafted in his answer. My answer is mostly a workaround for simple cases.

Upvotes: 1

Mike C.
Mike C.

Reputation: 601

Objective C only supports single inheritance. In this case you'd probably want to use protocols for common functionality. You can use a helper object to implement the protocol methods. In this case, you're not interested in whether your object is a member of a particular class, but whether it implements a specific set of methods.

Upvotes: 7

Daij-Djan
Daij-Djan

Reputation: 50089

you can't do that in Objective-C is single inheritance -- like java or most of the other modern languages.

use a paradigm of composition or delegation

Upvotes: 3

Related Questions