7.7. Protocols and Informal Protocols

Objective-C is a language with single inheritance, meaning that each class can have at most one superclass. But there is a mechanism for defining shared behavior between classes that are not related by inheritance: protocols. Protocols can be used to define a set of methods that a class must implement. It is a bit like a class without any data, consisting of an interface declaring the methods that are implemented, and nothing more. There is no limit to the number of protocols a class can conform to, and there is also no limit on the number of classes that can conform to a single protocol. With protocols, you can get some of the advantages of multiple inheritance, without the drawbacks.

NOTE

Programming languages generally fall into two basic categories: statically typed and dynamically typed. Statically typed languages such as C++ and Java require that the type of an object be explicitly known when the program is compiled, so that the compiler can check if it has been used in a valid way. This means that a programmer is often forced to explicitly cast object types in order to compile, making code more verbose and difficult to read. It also makes sending an arbitrary message to an object more involved, or, worse still, impossible. In C++, for example, it is not possible to send arbitrary messages to objects without manually building the capability into your program yourself.

Objective-C is a dynamically typed language, meaning that it does ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.