7.5. Subclassing

Each class in Objective-C can have a superclass from which it inherits data and methods. Only single inheritance is allowed, meaning that each class may only have a maximum of one superclass.

Languages like C++ allow multiple inheritance, where each class may have many superclasses. Other languages, such as Java, do not. Multiple inheritance is a hotly debated issue among OO programmers: it seems like a powerful feature, but you also have to be careful how you use it, or it can lead to serious design issues and make a program difficult to understand.

NOTE

The naming convention used in the example is that the getter shares the name of the instance variable, and the setter name begins with set, and ends with the variable name, in mixed-case format.

Naming of accessors in Objective-C/Cocoa programming is not simply a question of whatever takes your fancy. You should stick to the convention used here, because the Cocoa frameworks include various technologies, such as Bindings, that work only if your code adheres to the convention. Adopting a different naming scheme will effectively prevent you from using important features of Cocoa in your programs. You learn more about Bindings in Chapter 8.

Languages like Objective-C and Java have instead opted for single inheritance, but add a second mechanism to mimic one of the better aspects of multiple inheritance, namely, the ability to have two classes not in the same inheritance tree share some interface. Objective-C provides ...

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.