Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple superclasses in Objective-C?

Can I inherit from multiple classes in Objective-C? (If yes, how so?)

like image 688
Moshe Avatar asked Oct 07 '10 20:10

Moshe


People also ask

Is multiple inheritance possible in Objective C?

In keeping with its clean and simple design, Objective C does not support multiple inheritance, though features have been developed to replace some of the functionality provided by multiple inheritance (see run-time section below). The root of the Objective C class hierarchy is the Object class.

Can there be multiple Superclasses?

Object-Oriented ProgrammingMultiple inheritance means that a subclass can inherit from two or more superclasses. C++ allows multiple inheritance, but Java allows only single inheritance, that is, a subclass can inherit only one superclass.

How many Superclasses can a subclass have?

In Java, a subclass can only extend one superclass.


2 Answers

As others have said, Objective-C is single-inheritance. However, protocols provide handy ways to get around any type of situation you might have wanted multiple inheritance for and they allow you to avoid pitfalls that multiple inheritance creates such as The Diamond Problem.

Edit: Changes interface to protocol. Sorry, getting Java and Obj-C mixed up.

like image 68
Anthony Avatar answered Sep 30 '22 04:09

Anthony


No, Objective-C is single-inheritance only.

Consider looking at protocols which are a way of exposing a declared interface from a class. It's not the same as multiple inheritance, but solves some needs.

like image 31
Ben Zotto Avatar answered Sep 30 '22 02:09

Ben Zotto