12

Possible Duplicate:
Objective-C multiple inheritance

I want to implement multiple inheritance in Objective-C i.e. I have a class "Sub" which needs to be a sublass of class "Super" as well as UITableViewController

How can I acheive the same in Obj-C?

Community
  • 1
  • 1
hmthur
  • 1,032
  • 3
  • 13
  • 15

2 Answers2

22

Objective-C doesn't support multiple inheritance. You could use a protocol, composition and message forwarding to achieve the same result.

A protocol defines a set of methods that an object must implement (it's possible to have optional methods too). Composition is basically the technique of include a reference to another object and calling that object when it's functionality is required. Message forwarding is a mechanism that allows objects to pass messages onto other objects, for example, an object that is included via composition.

Apple Reference:

Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
5

There is no multiple inheritance in Objective-C. You can try to resolve what you need to with composition though.

Something like this:

@interface MyViewController : UIViewController {
    Sub *mySubComponent;
}
@property (nonatomic, retain) Sub *mySubComponent;
// you can write wrapper methods here to call mySubComponent methods/messages
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292