2

I have classA and ClassA inherit ClassX and access methods of classX. How can I also access the methiod of ClassY with inheritance because multiple inheritance is not possible. I dont want to create another other composition class for this.I want to use same classA for multiple inheritance.

9
  • I also have same issue to inherit multiple class. Commented Jul 9, 2015 at 16:19
  • 1
    This is a matter of how you're structuring your code. What you're asking is not possible, you need to re-think where you're putting your methods, and why you would need to inherit 2 different classes. Commented Jul 9, 2015 at 16:24
  • 2
    Your question appears to be "ObjC doesn't have multiple inheritance. How do I do multiple inheritance in ObjC?" The answer is in the question. You don't. What problem are you solving? Commented Jul 9, 2015 at 16:24
  • 1
    The answer is to use composition. You said you don't want to do that. It doesn't change that it's the answer to most Cocoa design questions. If it's off the table, then you probably cannot solve your underlying problem. Commented Jul 9, 2015 at 16:32
  • 1
    Many people think they are "supposed" to use multiple inheritance when they want to simply use a class's API/functionality. That is simply not the case. Inheritance should be for modeling an IS A relationship. It is one of the most overused and abused design patterns, so much so that in many cases it is an anti-pattern. Read a good design book, if it is a decent book it will steer you away from inheritance and towards composition. Commented Jul 9, 2015 at 16:46

3 Answers 3

4

There is no multiple inheritance. The only way to achieve this is by merging the two class heirarchies. Either by having ClassX inherit ClassY or ClassY inherit ClassX (then ClassA inherits the child class X or Y).

If the two classes do not by design fit into the same hierarchy, you might want to reconsider your design and the reasons why you do not want to use composition.

Sign up to request clarification or add additional context in comments.

6 Comments

Actually I am inheriting GoogleAdmodBanner Class in My class and accessing all methods and delegate, I also want to inherit GoogleInterstitialAd Class to access all methods and Delegate in that single class
It does not make sense that the same class is both a Banner and also an Interstitial at the same time (they have different appearances at the very least). Inheritance means IS-A. Unless you can say that "X is a type of Y" then X should not inherit from Y.
Why do you need both functionalities from the two classes in the same class you are implementing? Why not use separate classes?
Because Client wants in same class.
You need to educate your client.
|
2

Like Objective-C, Swift does not have multiple inheritance. Swift uses protocols and categories to give you the same sort of ability. You can define a protocol that defines a set of methods, and then you can add support for that protocol to multiple classes. You can often build support for a protocol into a category and then that category to your classes as needed.

Comments

1

As said before, multiple inheritance is not supported by the language (neither ObjC nor Swift). If you need to inherit methods/properties from multiple classes, you will need to use composition. Alternatively, what the language does allow you to do is to have a class conform to multiple protocols, which may or may not be a solution to the problem you are trying to solve.

I have come across very few cases where I thought that I really needed to have multiple inheritance, and even for those cases, they were typically resolved by employing an appropriate code design pattern (thinking about something like the Gang of Four design patterns). In essence, you want to abstract your code in such a way so that multiple inheritance is not a requirement anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.