Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a subclass inherit the protocols of its parent class in Objective-C?

Suppose I have a parent class that implements a protocol:

@interface GameViewController : UIViewController<GamePrizeDelegate> {
  ...
}

And then I make subclasses of it:

@interface TennisViewController : GameViewController {
  ...
}

@interface SoccerViewController : GameViewController {
  ...
}

Do I have to include the GamePrizeDelegate also in the subclasses? Are the protocols inherited as well?

Thanks!

like image 263
elitalon Avatar asked May 19 '11 12:05

elitalon


People also ask

Does Objective c have multiple inheritance?

It's more code, but there just isn't multiple inheritance as a language feature in objective-C. Composition is very often a better approach to take than inheritance, especially if you do much unit testing on the code.

What is inheritance in Objective C?

Inheritance allows us to define a class in terms of another class which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

Can parent class inherit the properties of child class?

I think No. Inheritance concept is to inherit properties from one class to another but not vice versa. But since parent class reference variable points to sub class objects. So it is possible to access child class properties by parent class object if only the down casting is allowed or possible....

Can all properties from parent class are accessible by subclass?

Subclass can not access parent private properties (fields) and methods. It can access public , protected and default properties and methods only. thank you !


1 Answers

Referring to Apple's documentation: Your subclass does inherit the adoption of the protocol, so you don't have to adopt it again.

Conforming to a Protocol

A class is said to conform to a formal protocol if it adopts the protocol or inherits from another class that adopts it. An instance of a class is said to conform to the same set of protocols its class conforms to.

like image 148
Nick Weaver Avatar answered Oct 18 '22 08:10

Nick Weaver