Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

32 HOUR 2: Object-Oriented Programming with Objective-C . Categories--Whereas protocols can be adopted by any class, categories consist of methods that are added to a specific class. You can use categories to add methods to an existing class for which you do not have the source code. . Extensions--Sometimes called anonymous categories, extensions are declared and implemented in the implementation of a class (typically the .m file). Extensions can add methods and properties to a class. They also can redefine properties (such as changing readonly to readwrite ). Summary In this hour you read about the design principle behind Objective-C as well as how it implements object-oriented programming objectives. In particular, you have seen how data abstraction and encapsulation are implemented in Objective-C so that you can write elegant, efficient, and maintainable object-oriented code. You have also seen how Objective-C implements a variety of architectures that allow for data reuse and sharing. With Objective-C, it is not just a matter of inheritance because you also have categories, extensions, and protocols to use. Q&A Q. Why do categories, extensions, and protocols matter in Objective-C? A. They allow you to reuse code in other ways than by creating subclasses. This can mean that your class hierarchies in Objective-C might be flatter than they are in languages in which the only way to share code it to subclass it. Q. Why does encapsulation matter? A. Along with data abstraction, it means that your objects are self-contained. The only attributes and functions that are exposed to the outside world are those that are common to all instances of the class. This can make ongoing mainte- nance easier.