votelessbubble
votelessbubble

Reputation: 805

Inheritance In Objective - C?

Okay, so my question itself states what i am asking.
I have a 3 view controllers. All the three of them are inheriting from a class (MainViewController).
Now what i want to ask is, is it possible in Objective C that suppose I have another class which has some variables and functions which i do not want to include in the MainViewController class, and pass this to one of the ViewControllers.
So if i number my view controllers, and let the other class be SecondClass.
1st, 2nd and 3rd ViewControllers inherit functions and variables from MainViewController.
If i want 2nd ViewController to inherit SecondClass also, then am i allowed to perform this kind of operation in Objective - C??

Upvotes: 0

Views: 240

Answers (3)

deltaaruna
deltaaruna

Reputation: 538

In objective C there is no support for multiple inheritance. Instead you can use either protocols or categories. In addition you can use dynamic typing as well. Check out following link. http://support.apple.com/kb/TA45894

Upvotes: 0

BlueBear
BlueBear

Reputation: 7639

Objective-C does not allow multiple inheritance. Check out this post for a great solution that uses composition.

Objective-C multiple inheritance

Upvotes: 2

Tommy
Tommy

Reputation: 100622

Objective-C is a single-inheritance language. Each class can inherit from exactly one superclass.

You can work around that with forwardingTargetForSelector: if you're absolute desperate but the neater and usually smarter thing is to compose functionality by saying that the second view controller owns an instance of SecondClass in addition to being a subclass of MainViewController.

You can see an example of Apple doing that in UIWebView. It isn't a subclass of UIScrollView but it uses a scroll view and exposes that instance (as of iOS 5) for outside actors via a property, scrollView.

Upvotes: 5

Related Questions