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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vivek Yadav
  • 1,117
  • 10
  • 14
  • I also have same issue to inherit multiple class. –  Jul 09 '15 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. – Chris Slowik Jul 09 '15 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? – Rob Napier Jul 09 '15 at 16:24
  • @ChrisSlowik 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. – Vivek Yadav Jul 09 '15 at 16:29
  • @RobNapier Yes Rob you are right I dont know about the implementation of multiple inheritance ,Please give me some suggestion about my just above comment .May be your suggestion help me out. – Vivek Yadav Jul 09 '15 at 16:31
  • 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. – Rob Napier Jul 09 '15 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. – Gruntcakes Jul 09 '15 at 16:46
  • @SvetlanaSlutstokyovich Can you please give me demo? – Vivek Yadav Jul 09 '15 at 16:47
  • Google for discussions or evaluations of composition over inheritance and you'll find lots of material for starters. Here's one at random from stack overflow http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance – Gruntcakes Jul 09 '15 at 16:49

3 Answers3

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.

Ahmed Nasser
  • 210
  • 1
  • 9
  • 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 – Vivek Yadav Jul 09 '15 at 16:33
  • 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. – Rob Napier Jul 09 '15 at 16:35
  • Why do you need both functionalities from the two classes in the same class you are implementing? Why not use separate classes? – Ahmed Nasser Jul 09 '15 at 16:35
  • Because Client wants in same class. – Vivek Yadav Jul 09 '15 at 16:36
  • You need to educate your client. – Ahmed Nasser Jul 09 '15 at 16:39
  • @AhmedNasser Thanks for your suggestion. Any other possibility? – Vivek Yadav Jul 09 '15 at 16:49
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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
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.

Luis Delgado
  • 3,644
  • 4
  • 34
  • 54