How to inherit from multiple classes

Say I have a griffin object that should be part of the felidae and bird class.
How to do it?
I can only inherit it from 1 class at a time ...

+6
objective-c
source share
5 answers

This can help...

Multiple inheritance

  • There is no innate multiple inheritance (of course, some find this useful). To get around this, you can create a composite class, that is, a class with instance variables that are identifiers of other objects. Instances can specifically redirect messages to any combination of objects that they stock. (This is not so much hassle, and you have direct control over the logic of inheritance.) [Of course, this is not a β€œproblem with the problem of the lack of multiple inheritance,” but just a simulation of your world, which is slightly different from such that you do not need multiple inheritance.]

  • Protocols to some extent describe the absence of multiple inheritance (MI): technically, protocols are equivalent to MI for purely "abstract" classes (see the answer to "Protocols" below).

  • [How does the Delegation fit here? Delegation extends the functionality of a class as expected by the developer of that class, without the need for a subclass. Of course, you can be a delegate to several objects of different classes. ]

-Selected from http://burks.brighton.ac.uk/burks/language/objc/dekorte/0_old/intro.htm

+14
source share

Multiple inheritance is not supported in Objective-C. The reason for not supporting this mechanism may be the fact that it would be too difficult to include in the language or the authors considered this to be poor programming and design decision. However, in various cases, multiple inheritance is useful. Fortunately, objective C does provide some workarounds for achieving multiple inheritance. The following are the options:

Option 1: Message Forwarding

Forwarding messages, as the name implies, is the mechanism offered by the Objective-C runtime. When a message is passed to an object and the object does not respond to it, the application crashes. But before the crash, the c runtime target provides a second chance for the program to pass the message to the corresponding object / class that actually responds to it. After tracking the message to the topmost superclass, the forwardInvocation message is forwardInvocation . By overriding this method, you can actually redirect the message to another class.

Example. If there is a Car class that has a carInfo property that provides the car brand, model and year of manufacture, and carInfo contains data in NSString format, it would be very useful if the methods of the NSString class could be called on objects of the Car class, which themselves actually inherited from NSObject.

 - (id)forwardingTargetForSelector:(SEL)sel { if ([self.carInfo respondsToSelector:sel]) return self.carInfo; return nil; } 

Source: iOS 4 Developer Cookbook - Erica Sadun

Option 2: Composition

Composition is a cocoa design template that includes binding to another object and calling its functions when required. Composition is actually a method for a presentation based on several other views. Thus, in cocoa terminology, this is very similar to subclassification.

 @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 

Source: Objective-C Multiple Inheritance

Option 3: Protocols

Protocols are classes that contain a method that will be implemented by other classes that implement the protocol. One class can implement as many protocols as it can and implement methods. However, with protocols, only methods can be inherited, not instance variables.

+7
source share

You can't, on your own. But you can have links to as many other objects as you need, and you can use several protocols.

+3
source share

You can dynamically create a class at runtime and select methods for each parent class to inherit from. Take a look at the NeXT runtime documentation here on dynamically creating classes. I did this once just for fun, but I didn’t get very far, since it gets incredibly dirty very quickly.

Edit

This gets trickier because there can only be one superclass, otherwise the super keyword becomes ambiguous.

+3
source share

First, make felidae a subclass of the bird. Piece of cake. :-)

+1
source share

All Articles