<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/Object-Oriented_Programming_with_Objective-C&amp;action=history&amp;feed=atom</id>
		<title>Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/Object-Oriented_Programming_with_Objective-C&amp;action=history&amp;feed=atom"/>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/Object-Oriented_Programming_with_Objective-C&amp;action=history"/>
		<updated>2013-06-19T09:13:55Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.11.0</generator>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/Object-Oriented_Programming_with_Objective-C&amp;diff=6262&amp;oldid=prev</id>
		<title>Docbook2Wiki: Initial conversion from Docbook</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/Object-Oriented_Programming_with_Objective-C&amp;diff=6262&amp;oldid=prev"/>
				<updated>2008-03-07T12:56:06Z</updated>
		
		<summary type="html">&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Learning Cocoa with Objective-C/TOC}}&lt;br /&gt;
Object-oriented programming isn't rocket science, but you can't learn it overnight either. There is a lot of terminology—composed of words like &amp;quot;encapsulation&amp;quot; and &amp;quot;polymorphism&amp;quot; and phrases like &amp;quot;is-a&amp;quot; and &amp;quot;has-a&amp;quot;—that goes with the territory. The concepts behind these terms are not terribly complicated, but they can be hard to explain. Like most useful fields of study, you must work with it a while before it all clicks together in your mind. As each concept becomes clear, you will gain a deeper understanding of the subject. That said, you don't have to understand everything about object-oriented programming on the first pass to make good use of the concepts.&lt;br /&gt;
&lt;br /&gt;
In this chapter, we present the object-oriented concepts that matter most when working with Cocoa, along with quite a bit of hands-on practice using those concepts. If this is the first time you've approached object-oriented programming, read carefully, but don't worry if you don't get everything at first. Just remember to flip back to this part of the book later if something didn't sink in. If you already know a bit about object-oriented programming, then you should treat this as a refresher and see how Objective-C's implementation of the object-oriented concepts with which you are familiar works.&lt;br /&gt;
&lt;br /&gt;
== Introducing Objects ==&lt;br /&gt;
&lt;br /&gt;
Procedural programming divides the programming problem into two parts: data and operations on that data. Because all of the functionality of a procedural program works on the same set of data, the programmer must be very careful to manipulate the data of a program in such a way that the rest of the program can work correctly. He must be aware of the entire program at a low level of abstraction so as not to introduce errors. As a procedural program grows in size, the network of interaction between procedures and data becomes increasingly complex and hard to manage.&lt;br /&gt;
&lt;br /&gt;
Object-oriented programming (OOP), first developed in the 1960s,&amp;lt;ref&amp;gt;SIMULA I and SIMULA 67 were the first two object-oriented programming languages. They were designed and built by Ole-Johan Dahl and Kristen Nygaard in Norway between 1962 and 1967.&amp;lt;/ref&amp;gt; restructures the programming problem to allow for a higher level of abstraction. It groups operations and data into modular units called ''objects''. These objects can be combined into structured networks to form a complete program, similar to how the pieces in a puzzle fit together to create a picture. In contrast to procedural programming's focus on the interaction between data and functions, the design of objects and the interactions between those objects become the primary elements of object-oriented program design.&lt;br /&gt;
&lt;br /&gt;
By breaking down complex software projects into small, self-contained, and modular units, object orientation ensures that changes to one part of a software project will not adversely affect other portions of the software. Object orientation also aids software reuse. Once functionality is created in one program, it can easily be reused in other programs.&lt;br /&gt;
&lt;br /&gt;
Programming with objects is quite like working with real-world objects. Take an iPod, for example. It embodies both state and behavior. When you operate it, you don't necessarily care how it works, as long as it works in the way that you expect. As long as your iPod plays music when you tell it to and synchronizes your music collection with iTunes when you plug it into your computer, you're happy. Object-oriented programming brings this same level of abstraction to programming and helps remove some of the impediments to building larger systems. To enjoy listening to music, you don't have to know that iTunes and your iPod use the MP3 format; you just put a CD into your computer and import the music into your collection. iTunes and your iPod work together to download the music from your computer when you plug in the iPod. [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-1|Figure 3-1]] shows these components working together.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-1. Real-world objects interacting together'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt49.png|Real-world objects interacting together]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Classes of Objects ===&lt;br /&gt;
&lt;br /&gt;
In the real world, there are often many objects of the same kind, or ''type''. My iPod is just one of many iPods that exist in the world. In the lingo of object-oriented programming, each iPod is an ''instance''. An instance of an object has its own state and leads an existence independent of all other instances. My iPod probably has a very different collection of music than yours does.&amp;lt;ref&amp;gt;There's even a decent chance that you might not like the music on my iPod, and vice versa.&amp;lt;/ref&amp;gt; But just as all iPods have the same set of buttons — allowing the same set of operations (play, stop, etc.) — all instances of a particular object expose the same functionality to the outside world.&lt;br /&gt;
&lt;br /&gt;
You specify an object by defining its ''class''. Think of a class as a blueprint for making object instances. It provides all the information needed to build new instances of an object. Each class defines the internal variables that hold the data of an object instance and the ways, or ''methods'' , by which that data can be manipulated. These methods define the ''interface'' of the object. The interface is how other objects are allowed to use it.&lt;br /&gt;
&lt;br /&gt;
On the back of every iPod is the phrase &amp;quot;Designed by Apple in Cupertino. Assembled in Taiwan.&amp;quot; This is a useful analogy for thinking about how classes and objects relate to each other. In its corporate offices in California, Apple defined how an iPod operates and what kinds of data it can store. Apple shipped those definitions to the factory in Taiwan that now creates many unique instances of an iPod to ship to customers around the world. When you create a class, you create a definition from which the ''runtime'' (the layer of software that enables the object-oriented system to run) can create any number of objects (see [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-2|Figure 3-2]]).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-2. Runtime creates object instances from a class'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt50.png|Runtime creates object instances from a class]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Objective-C, classes are more than just blueprints. They are actually first-class objects themselves that can have methods associated with the class and not with its instances. These are called ''class methods'' . Every object created has a reference to its own class. The iPod analogy starts to get a bit stretched here, but imagine that each iPod had a reference to the plans on which it was based and could consult them at any time. This is sort of what it means for an object to look up its class object anytime it needs to do so.&lt;br /&gt;
&lt;br /&gt;
=== Inheritance ===&lt;br /&gt;
&lt;br /&gt;
We've defined a class to be a definition, or blueprint, from which object-oriented instances are created. An iPod is an instance of the iPod class. But classes themselves can be defined as specializations of other classes. For example, if you didn't know what an iPod was, you would probably understand if I told you that it was a handheld MP3 player. In fact, all handheld MP3 players share a certain number of characteristics. Like an iPod, a Rio can hold and play MP3 files downloaded from a computer. It can't hold as many songs as the iPod, but at least some of the functionality is the same.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
The iPod is actually much more than a portable MP3 player. It's also a bootable FireWire drive that can hold any kind of data that you want it to hold. People are finding some pretty creative uses for it beyond playing music. In Objective-C, objects that can perform other functions can declare that they obey a particular ''protocol'' , or way of behaving. We'll talk more about protocols and how they can be used effectively in [[Learning Cocoa with Objective-C/Single-Window Applications/Models and Data Functionality|Chapter 9]].&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Object-oriented programming lets us collect similar functionalities of different classes and group them into a common parent class through ''inheritance''. We can say that an iPod and a Rio are both types of MP3 players. If we define a common &amp;lt;tt&amp;gt;MP3Player&amp;lt;/tt&amp;gt; class, we can gather certain aspects common to both devices into one class, as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-3|Figure 3-3]].&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;iPod&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;Rio&amp;lt;/tt&amp;gt; classes are both ''subclasses'' of the &amp;lt;tt&amp;gt;MP3Player&amp;lt;/tt&amp;gt; class. Likewise, the &amp;lt;tt&amp;gt;MP3Player&amp;lt;/tt&amp;gt; class is the ''superclass'' of the &amp;lt;tt&amp;gt;iPod&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;Rio&amp;lt;/tt&amp;gt; classes. Each subclass inherits state (in the form of variable definitions) and functionality from the superclass. In this case, both players inherit the same basic functions (play, stop, fast forward, etc.), but have very different underlying implementations. The iPod uses a high-capacity hard drive while the Rio uses flash memory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-3. Class hierarchy for the MP3Player class'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt51.png|Class hierarchy for the MP3Player class]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating a new class is often a matter of specialization. Since the new class inherits all of its superclass's behavior, you don't need to reimplement the things that work in the way that you want. The subclass merely extends the inherited behavior by adding new methods and any variables needed to support the additional methods. A subclass can alter superclass behavior by overriding an inherited method, reimplementing the method to achieve a behavior different from the superclass's implementation.&lt;br /&gt;
&lt;br /&gt;
With Objective-C, a class can have any number of subclasses, but only one superclass.&amp;lt;ref&amp;gt;Some object-oriented programming languages, such as C++, allow classes to inherit functionality from more than one superclass. This ability, known as multiple-inheritance, can often lead to more problems than it solves. Objective-C provides protocols (discussed in [[Learning Cocoa with Objective-C/Single-Window Applications/Models and Data Functionality|Chapter 9]]) to provide some of the benefits of sharing behavior (but not implementation) across the class hierarchy.&amp;lt;/ref&amp;gt; This means that classes are arranged in a branching hierarchy with one class at the top—the root class that has no superclass—as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-4|Figure 3-4]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-4. The root class in Objective-C'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt52.png|The root class in Objective-C]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; is the root class of this hierarchy. From &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;, other classes inherit the basic functionality that lets them work in the system. The root class also creates a framework for the creation, initialization, deallocation, introspection, and storage of objects.&lt;br /&gt;
&lt;br /&gt;
As noted earlier, you often create a subclass of another class because that superclass provides most, but not all, of the behavior that you require. A subclass can have its own unique purpose that does not build on the role of an existing class. To define a new class that doesn't need to inherit any special behavior other than the default behavior of objects, you make it a subclass of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a powerful concept—one that many people new to object-oriented programming tend to use too much. Used inappropriately, it can lead to fragile software. In Cocoa, it's often easier to use a new set of classes from a new class than to use inheritance. This is called ''object composition'' . As you work through this book, you'll see many examples of object composition.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating and Using Objects ==&lt;br /&gt;
&lt;br /&gt;
Now that we've introduced a few object-oriented concepts, we are going to dive into some simple code exercises to show how to apply this knowledge. The following steps will guide you:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation tool (File → New Project → Tool → Foundation Tool) project named &amp;quot;objects&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Next, modify the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, so that it looks like [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-1|Example 3-1]]. The Foundation tool project template automatically generates some of this code. The lines that you need to add are shown in '''boldface''' type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-1. Creating objects'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoReleasepool *pool = [[NSAutoreleasepool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''    NSObject * object;                                               // a'''&lt;br /&gt;
                         '''    object = [NSObject alloc];                                       // b'''&lt;br /&gt;
                         '''    object = [object init];                                          // c'''&lt;br /&gt;
                         '''    NSLog(@&amp;quot;Created object: %@&amp;quot;, object);                            // d'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's what the code that we added does:&lt;br /&gt;
&lt;br /&gt;
# Declares a variable named &amp;lt;tt&amp;gt;object&amp;lt;/tt&amp;gt; of type &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;. You should recognize this as a regular C pointer.&lt;br /&gt;
# Creates a new object of type &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; and assigns it to the &amp;lt;tt&amp;gt;object&amp;lt;/tt&amp;gt; variable. The &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method reserves (or allocates) memory space for the object and returns a pointer to that space. We'll explain more about methods in just a bit.&lt;br /&gt;
# Before an object is used in any way, it must be initialized. This &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; call initializes the object so it can be used. The &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method returns a fully initialized object ready for use. Since it is possible that the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method will return a different object, we assign the return to the &amp;lt;tt&amp;gt;object&amp;lt;/tt&amp;gt; variable again.&lt;br /&gt;
# Prints a representation of the object to the console using a &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; style format string with a &amp;lt;tt&amp;gt;%@&amp;lt;/tt&amp;gt; token, indicating that the svalue of the object given after the format string should be printed.&lt;br /&gt;
&lt;br /&gt;
There's actually a bit more going on in this code than what we've described. However, we'll fill in the missing pieces as we go to avoid introducing too many concepts at once.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Format String Tokens'''&lt;br /&gt;
&lt;br /&gt;
There are several methods in Cocoa, such as &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;[NSString stringWithFormat]&amp;lt;/tt&amp;gt;, that can use format strings with a list of arguments. These format strings can contain all of the normal &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt;-style tokens, as well as a Cocoa-specific token for objects. You'll find the following tokens to be useful:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;%@&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Print as an object&lt;br /&gt;
;&amp;lt;tt&amp;gt;%d&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;%i&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Print as a signed decimal&lt;br /&gt;
;&amp;lt;tt&amp;gt;%o&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Print as an unsigned octal&lt;br /&gt;
;&amp;lt;tt&amp;gt;%s&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Print as a string&lt;br /&gt;
;&amp;lt;tt&amp;gt;%u&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Print as an unsigned decimal&lt;br /&gt;
;&amp;lt;tt&amp;gt;%x&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Print as an unsigned hexadecimal&lt;br /&gt;
&lt;br /&gt;
In addition to the tokens in this short list, you can add all sorts of modifiers to control precisely how values are printed. See the &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; manpage for complete information about format strings. You can access the manpage in either of the following ways:&lt;br /&gt;
&lt;br /&gt;
* Open a Terminal window and enter &amp;lt;tt&amp;gt;'''man printf'''&amp;lt;/tt&amp;gt;at the prompt.&lt;br /&gt;
* In Project Builder, use the Help → Open man page... menu item.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run the program. You should see something like this on the console:&lt;br /&gt;
&lt;br /&gt;
 2002-06-11 23:17:16.181 objects[477] Created object: &amp;lt;NSObject: 0x5ae90&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells us that we created an object of type &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; that is located at the memory address 0x5ae90. This isn't the most exciting information that could be printed, and it certainly won't win any user-interface awards, but it shows us that objects are being created in the system by the runtime.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
As a Cocoa programmer, you probably won't ever make direct use of the memory location of the object instances you create. But under the hood, Cocoa uses this information to locate and manipulate objects that you reference in code.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since objects should never be used without proper allocation and initialization, Objective-C programmers tend to combine the methods into one line as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-2|Example 3-2]]. Replace lines a, b, and c from [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-1|Example 3-1]] with the single bolded line in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-2|Example 3-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-2. Combing object allocation and initialization'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoReleasepool *pool = [[NSAutoreleasepool alloc] init];&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;NSObject * object = [[NSObject alloc] init];&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
     NSLog(@&amp;quot;Created object: %@&amp;quot;, object);&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This shortens the allocation and initialization of an object to one line, ensuring that everything works properly, even in the case where the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method of a class returns a different object than originally allocated. We will use this style of object creation throughout the rest of the book.&lt;br /&gt;
&lt;br /&gt;
=== Working with Multiple Objects ===&lt;br /&gt;
&lt;br /&gt;
Working with multiple object instances of the same class is easy, as long as you keep the references to different objects distinct.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Edit the code in the project's ''main.m'' file as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-3|Example 3-3]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-3. Working with multiple objects'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoReleasepool *pool = [[NSAutoreleasepool alloc] init];&lt;br /&gt;
&lt;br /&gt;
    NSObject * object&amp;lt;/nowiki&amp;gt;'''1'''&amp;lt;nowiki&amp;gt; = [[NSObject alloc] init]; &lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;    NSObject * object2 = [[NSObject alloc] init];&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
     NSLog(@&amp;quot;object'''1''': %@&amp;quot;, object'''1'''); &lt;br /&gt;
     '''NSLog(@&amp;quot;object2: %@&amp;quot;, object2);'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;When built and run, the program will print something similar to the following:&lt;br /&gt;
&lt;br /&gt;
 2002-06-11 15:59:29.716 objects[370] object1: &amp;lt;NSObject: 0x4ce90&amp;gt;&lt;br /&gt;
 2002-06-11 15:59:29.717 objects[370] object2: &amp;lt;NSObject: 0x4b410&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example shows that two object instances of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; have been allocated, and they occupy two different locations in memory.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Methods and Messages ==&lt;br /&gt;
&lt;br /&gt;
In our discussion about objects so far, we've been using (and promised to explain) the term ''method'' . Methods are structured like C functions and can be thought of as procedures; but, instead of being global in nature, they are procedures associated with and implemented by the object's class.&lt;br /&gt;
&lt;br /&gt;
There are two kinds of methods: ''class methods'' and ''instance methods'' . Class methods are scoped to the class itself and cannot be called on instances of the class. The &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method is an example of a class method. Instance methods, on the other hand, are scoped to object instances. The &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method is an example of an instance method that is called on an instance of an object returned by the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
To call a method, send an object a ''message'' telling it to apply a method. All those square brackets that you have seen in the code are message expressions that result in methods being called. [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-5|Figure 3-5]] shows the various parts of a basic message.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-5. Objective-C message expression'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt55.png|Objective-C message expression]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this figure, the message is the expression enclosed in square brackets to the right of the assignment operator (equals sign). The message consists of an object, known as a ''receiver'' , and the name of a method to call on that object. In this case, the object is the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class, and the method to be called is the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method. In response to receiving this message, the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class returns a new instance of the class that will be assigned to the variable &amp;lt;tt&amp;gt;anObject&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Arguments in Messages ===&lt;br /&gt;
&lt;br /&gt;
The message in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-5|Figure 3-5]] calls a method that doesn't take any arguments. Like procedures, methods can receive multiple arguments. In Objective-C, every message argument is identified with a label (a colon-terminated keyword), which is considered part of the method name. [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-6|Figure 3-6]] shows a message with a single argument.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-6&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-6. Objective-C message expression with a single argument'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt56.png|Objective-C message expression with a single argument]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this figure, the message tells the runtime to call the &amp;lt;tt&amp;gt;setWidth:&amp;lt;/tt&amp;gt; method and pass it the argument &amp;lt;tt&amp;gt;width&amp;lt;/tt&amp;gt;. Notice that a colon terminates method names that take an argument, while method names that don't take an argument (like the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-5|Figure 3-5]]) don't have a colon.&lt;br /&gt;
&lt;br /&gt;
[[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-7|Figure 3-7]] shows a multiple-argument message. Here, the message and arguments are used to set the width and height of the rectangle object to &amp;lt;tt&amp;gt;width&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;height&amp;lt;/tt&amp;gt;, respectively. This method is called the &amp;lt;tt&amp;gt;setWidth:height:&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-7&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-7. Objective-C message expression with multiple arguments'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt57.png|Objective-C message expression with multiple arguments]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
Note that &amp;lt;tt&amp;gt;setWidth:height:&amp;lt;/tt&amp;gt; refers to one method, not two. It will call a method with two arguments. When using this method, you must pass in two arguments, labeled and formatted as in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-6|Figure 3-6]]. If you haven't used Smalltalk or one of its derivatives, you will find this practice strange at first, but you'll soon appreciate the readability it imparts to code.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Nested Messages ===&lt;br /&gt;
&lt;br /&gt;
[[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-8|Figure 3-8]] shows nested messages. By enclosing one message within another, you can use a returned value as an argument without having to declare a variable for it. The innermost message expression is evaluated first, resulting in a return object. Then the next nested message expression is evaluated using the object that was returned in the inner expression as the receiver of the second message. We saw this in action in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-2|Example 3-2]] when we combined the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; methods of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-8&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-8. Nested Objective-C messages'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt58.png|Nested Objective-C messages]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
Nested messages work only when the inner expression returns an object value. If the inner expression returns something else (for example, an &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;) then a crash will result at runtime. This is because messages can be passed only to objects, not to primitive types.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How Messaging Works ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class ensures that every object in the system has an instance variable named &amp;lt;tt&amp;gt;isa&amp;lt;/tt&amp;gt; . This variable points to the class that defines how the object works.In addition, every class object has a reference to its superclass. This relationship is illustrated in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-9|Figure 3-9]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-9&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-9. Instances have a reference to their class.'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt59.png|Instances have a reference to their class.]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The class object contains quite a bit of information about the internals of the class and how it works. Part of this information is a method lookup table that maps ''selectors'' to methods, as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-10|Figure 3-10]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-10&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-10. Method lookup table'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt60.png|Method lookup table]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A selector (defined as the &amp;lt;tt&amp;gt;SEL&amp;lt;/tt&amp;gt; type in Objective-C) is a compiler-assigned code that identifies a method to the runtime. When you send a message to an object, the compiler actually creates code to perform a call to an Objective-C-defined function, which uses the selector information to perform a dynamic method lookup at runtime. For more details about how this functionality works underneath the hood, read ''Inside Mac OS X: The Objective-C Language'', located in the ''/Developer/Documentation /Cocoa/ObjectiveC''folder.&lt;br /&gt;
&lt;br /&gt;
== Objective-C-Defined Types ==&lt;br /&gt;
&lt;br /&gt;
So far, we've talked about a few of Objective-C's built-in types, such as &amp;lt;tt&amp;gt;SEL&amp;lt;/tt&amp;gt;. Before we continue, [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-TABLE-1|Table 3-1]] lists the set of Objective-C-defined types.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-TABLE-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-1. Objective-C-defined types'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Type !! Definition&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 id&lt;br /&gt;
| An object reference (a pointer to its data structure)&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Class&lt;br /&gt;
| A class object reference (a pointer to its data structure)&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 SEL&lt;br /&gt;
| A selector (a compiler-assigned code that identifies a method name)&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 IMP&lt;br /&gt;
| A pointer to a method implementation that returns an id&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 BOOL&lt;br /&gt;
| A Boolean value, either &amp;lt;tt&amp;gt;YES&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;NO&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 nil&lt;br /&gt;
| A null object pointer, &amp;lt;tt&amp;gt;(id)0&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Nil&lt;br /&gt;
| A null class pointer, &amp;lt;tt&amp;gt;(Class)0&amp;lt;/tt&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; type can be used to type any kind of object, class, or instance. In addition, class names can be used as type names to type instances of a class statically. A statically typed instance is declared as a pointer to an instance of its class or to an instance of any class from which it inherits.&lt;br /&gt;
&lt;br /&gt;
== Creating New Classes ==&lt;br /&gt;
&lt;br /&gt;
When you want to create a new kind of object, you define a new class. A class is defined in two files. One file, the ''header file'' (''.h''), declares the variables and methods that can be invoked by messages sent to objects belonging to the class. The other file is the ''implementation file'' (''.m''), which actually implements the methods declared by the header file, as well as the private implementation details of the class. The interface defined in the header file is public. The implementation is private and can be changed without affecting the interface or the way the class is used.&lt;br /&gt;
&lt;br /&gt;
To show how to create a new class, we will model songs that would go into an MP3 player. Don't get too scared yet; we're not actually going to write the MP3 player itself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;To get started, create a new Foundation Tool in Project Builder (File → New Project → Tools → Foundation Tool) named &amp;quot;songs&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Define a header for our song class. Choose File → New File, then select Objective-C class as the file type, as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-11|Figure 3-11]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-11&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-11. New File Assistant'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt68.png|New File Assistant]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Name the file ''Song.m'',as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-12|Figure 3-12]]. Make sure that the Also create &amp;quot;Song.h&amp;quot; checkbox is clicked. This creates the header file for the application's interface.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-12&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-12. New Objective-C class Assistant'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt69.png|New Objective-C class Assistant]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
Be careful not to confuse this use of the word ''interface'' with the term ''Graphical User Interface''. This use of the word refers to how components talk, or know, about each other and doesn't refer to how users will interact with the program.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you finish, Project Builder should look something like [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-FIG-13|Figure 3-13]]. If ''Song.h''and ''Song.m''are not in the Source category of files, you can simply drag them there. (Hint: Use the black insertion indicator that appears in the outline view to guide you as you drag.) Where they appear doesn't matter to Project Builder, but keeping things neat and tidy will help you, especially on larger projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-FIG-13&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 3-13. Newly created class in Project Builder'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_3_tt70.png|Newly created class in Project Builder]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By creating the class header and implementation files, we have a start on a class that can be used in the rest of the program, including our main function in the ''main.m'' file. Project Builder creates a basic ''Song.h'' header file for you. A new class is declared in the header file with the &amp;lt;tt&amp;gt;@interface&amp;lt;/tt&amp;gt; compiler directive. In this case, the directive is the following:&lt;br /&gt;
&lt;br /&gt;
 @interface Song : NSObject&lt;br /&gt;
&lt;br /&gt;
This indicates that we are defining a class called &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; that inherits from &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;. The colon indicates the inheritance. The rest of the file is left for us to complete. All the instance variables used by the class are declared between the brackets. All the methods of the class are declared between the end bracket and the &amp;lt;tt&amp;gt;@end&amp;lt;/tt&amp;gt; directive.&lt;br /&gt;
&lt;br /&gt;
=== Defining the Song Interface ===&lt;br /&gt;
&lt;br /&gt;
Edit the ''Song.h''file as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-4|Example 3-4]]. Once again, the lines that you need to add are shown in '''boldface''' type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-4. Song.h interface'''&lt;br /&gt;
&lt;br /&gt;
 @interface Song : NSObject {&lt;br /&gt;
 '''    NSString * name;                                                           // a'''&lt;br /&gt;
 '''    NSString * artist;                                                         // b'''&lt;br /&gt;
 }&lt;br /&gt;
 '''- (NSString *)name;                                                            // c'''&lt;br /&gt;
 '''- (void)setName:(NSString *)newName;                                           // d'''&lt;br /&gt;
 '''- (NSString *)artist;                                                          // e'''&lt;br /&gt;
 '''- (void)setArtist:(NSString *)newArtist;                                       // f'''&lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's what the additional code does:&lt;br /&gt;
&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; variable that will point to an object of type &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; variable that will point to an object of type &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Declares an instance method, named &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt;, that returns a pointer to an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object when called.&lt;br /&gt;
# Declares an instance method, named &amp;lt;tt&amp;gt;setName:&amp;lt;/tt&amp;gt;, that takes a pointer to an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object as an argument. The minus sign at the start of the method declaration indicates that it's an instance method, as opposed to a class method. This method will be used to set the name of the song that the object represents. The method does not return anything, so we declare it to return &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Declares an instance method, named &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt;, that returns a pointer to an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object when called.&lt;br /&gt;
# Declares an instance method, named &amp;lt;tt&amp;gt;setArtist:&amp;lt;/tt&amp;gt;, that takes a pointer to an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object as an argument. This method will be used to set the artist of the song that the object represents. Once again, this method does not return anything, so we declare it to return &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
In our example so far, we've only defined instance methods using the minus sign (-). Class methods, such as the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;, are defined in code using the plus sign (&amp;lt;tt&amp;gt;+&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Properties and Accessor Methods'''&lt;br /&gt;
&lt;br /&gt;
Notice that in our ''Song.h'' file we have two methods for each variable, which are named using a simple pattern. The variable is known as a ''property'' . The methods are known as ''accessor methods'' . The method that is named after the property returns the value of the property to the caller. The method named &amp;lt;tt&amp;gt;setProperty&amp;lt;/tt&amp;gt; changes the property. In the case of our &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class, the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; method gets the name of the Song, and the &amp;lt;tt&amp;gt;setName&amp;lt;/tt&amp;gt; method sets it. Hiding access to the properties of the object is called ''encapsulation''; this allows you to change the implementation of how variables are stored inside the object without potentially breaking code elsewhere.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Defining the Song Implementation ===&lt;br /&gt;
&lt;br /&gt;
Now that we have defined the interface, we actually need to fill in the implementation of the class. Take a look at the ''Song.m''file. You will notice that it imports ''Song.h'', which is the application's interface. There are also two compiler directives, &amp;lt;tt&amp;gt;@implementation Song&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;@end&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Add the code shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-5|Example 3-5]] between the &amp;lt;tt&amp;gt;@implementation&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;@end&amp;lt;/tt&amp;gt; directives.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-5. The Song.m implementation'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;quot;Song.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 @implementation Song&lt;br /&gt;
 &lt;br /&gt;
 '''- (NSString *)name                              // a'''&lt;br /&gt;
 '''{ '''&lt;br /&gt;
 '''    return name;                                                      // b'''&lt;br /&gt;
 '''} '''&lt;br /&gt;
 &lt;br /&gt;
 '''- (void)setName:(NSString *)newName                                   // c'''&lt;br /&gt;
 '''{'''&lt;br /&gt;
 '''     [newName retain];                                                // d'''&lt;br /&gt;
 '''    [name release];                                                   // e'''&lt;br /&gt;
 '''    name = newName;                                                   // f'''&lt;br /&gt;
 '''}'''&lt;br /&gt;
 &lt;br /&gt;
 '''- (NSString *)artist                                                  // g'''&lt;br /&gt;
 '''{'''&lt;br /&gt;
 '''    return artist;                                                    // h'''&lt;br /&gt;
 '''}'''&lt;br /&gt;
 &lt;br /&gt;
 '''- (void)setArtist:(NSString *)newArtist                               // i'''&lt;br /&gt;
 '''{'''&lt;br /&gt;
 '''    [newArtist retain];                                               // j'''&lt;br /&gt;
 '''    [artist release];                                                 // k'''&lt;br /&gt;
 '''    artist = newArtist;                                               // l'''&lt;br /&gt;
 '''}'''&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's what the additional code does:&lt;br /&gt;
&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; method that returns an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; return value.&lt;br /&gt;
# Returns the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object associated with the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; instance variable.&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;setName:&amp;lt;/tt&amp;gt; method that takes a single &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; argument.&lt;br /&gt;
# Sends the &amp;lt;tt&amp;gt;retain&amp;lt;/tt&amp;gt; message to the &amp;lt;tt&amp;gt;newName&amp;lt;/tt&amp;gt; object. This tells the object that we intend to keep a reference to it. This is part of Cocoa's memory management that will be described in depth in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit|Chapter 4]].&lt;br /&gt;
# Sends the &amp;lt;tt&amp;gt;release&amp;lt;/tt&amp;gt; message to the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; object. If the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; object is not pointing to an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object (if it is pointing to &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;), then this message will not do anything. However, if &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; had been set on this &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; object before, this message would tell the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object that we were not interested in it anymore.&lt;br /&gt;
# Sets the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; variable to point to the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object to which &amp;lt;tt&amp;gt;newName&amp;lt;/tt&amp;gt; points.&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; method that returns an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; return value.&lt;br /&gt;
# Returns the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object associated with the &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; instance variable.&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;setArtist:&amp;lt;/tt&amp;gt; method that takes a single &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; argument.&lt;br /&gt;
# Sends the retain message to the &amp;lt;tt&amp;gt;newArtist&amp;lt;/tt&amp;gt; object, telling it that we are interested in keeping a reference to it.&lt;br /&gt;
# Sends a release message to the existing object to which our &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; variable points, if any.&lt;br /&gt;
# Sets the artist variable to point to the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object to which &amp;lt;tt&amp;gt;newArtist&amp;lt;/tt&amp;gt; points.&lt;br /&gt;
&lt;br /&gt;
=== Using the Song Class ===&lt;br /&gt;
&lt;br /&gt;
Now, we need to edit the main function in the ''main.m''file, so we can do something with the &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Edit the ''main.m'' file to match [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-6|Example 3-6]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-6&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-6. Using the Song class'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;br /&gt;
 '''#import &amp;quot;Song.h&amp;quot;                                     // a'''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;    Song * song1 = [[Song alloc] init];              // b&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                            '''    [song1 setName:@&amp;quot;We Have Explosive&amp;quot;];'''&lt;br /&gt;
                            '''    [song1 setArtist:@&amp;quot;The Future Sound of London&amp;quot;];'''&lt;br /&gt;
 &lt;br /&gt;
                            '''&amp;lt;nowiki&amp;gt;    Song * song2 = [[Song alloc] init];              // c&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                            '''    [song2 setName:@&amp;quot;Loops of Fury&amp;quot;];'''&lt;br /&gt;
                            '''    [song2 setArtist:@&amp;quot;The Chemical Brothers&amp;quot;];'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    NSLog(@&amp;quot;Song 1: %@&amp;quot;, song1);                     // d'''&lt;br /&gt;
                            '''    NSLog(@&amp;quot;Song 2: %@&amp;quot;, song2);'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's what the additional code does:&lt;br /&gt;
&lt;br /&gt;
# Imports the ''Song.h'' interface file, so we can use the &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
# Allocates, initializes, and sets the name (&amp;lt;tt&amp;gt;setName&amp;lt;/tt&amp;gt;) and artist (&amp;lt;tt&amp;gt;setArtist&amp;lt;/tt&amp;gt;) of &amp;lt;tt&amp;gt;song1&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; methods work just the same as they did with &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;, since &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; inherits them from &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Allocates, initializes, and sets the name and artist of &amp;lt;tt&amp;gt;song2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Prints the &amp;lt;tt&amp;gt;song1&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;song2&amp;lt;/tt&amp;gt; objects, so we can see them.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run the program. You should see something like this on the console:&lt;br /&gt;
&lt;br /&gt;
 2002-06-11 22:05:11.866 songs[7058] Song 1: &amp;lt;Song: 0x50f30&amp;gt;&lt;br /&gt;
 2002-06-11 22:05:11.867 songs[7058] Song 2: &amp;lt;Song: 0x4f4b0&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will recognize that this output is similar to the output that was printed from the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; object instances. This is because the &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; method actually calls the &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method on an object, as defined by the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class. To change this to print something a bit more user-friendly, we somehow need to redefine what the &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method prints .&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
If you get a compiler error saying that the &amp;lt;tt&amp;gt;song2&amp;lt;/tt&amp;gt; variable is undeclared, chances are that you are not using Mac OS X 10.2 (Jaguar). This book makes use of many of the new features of Jaguar, including support for the C99 standard in GCC 3.1.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Overriding Methods ==&lt;br /&gt;
&lt;br /&gt;
A subclass can not only add new methods to the ones it inherits from a superclass; it can also replace, or ''override'', an inherited method with a new implementation. No special syntax is required; just reimplement the method in the subclass's implementation file.&lt;br /&gt;
&lt;br /&gt;
Overriding methods doesn't alter the set of messages that an object can receive. It alters the method implementation that will be used to respond to those messages. This ability for each class to implement its own version of a method is known as ''polymorphism'' .&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Edit the ''Song.m''file as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-7|Example 3-7]] to add the &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-7&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-7. Adding the description method'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;quot;Song.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 @implementation Song&lt;br /&gt;
 &lt;br /&gt;
 - (NSString *)name&lt;br /&gt;
 {&lt;br /&gt;
     return name;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (void)setName:(NSString *)newName                                 &lt;br /&gt;
 {&lt;br /&gt;
     [newName retain];                                               &lt;br /&gt;
     [name release];                                               &lt;br /&gt;
     name = newName;                                                 &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (NSString *)artist&lt;br /&gt;
 {&lt;br /&gt;
     return artist;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (void)setArtist:(NSString *)newArtist                            &lt;br /&gt;
 {&lt;br /&gt;
     [newArtist retain];                                            &lt;br /&gt;
     [artist release];                                           &lt;br /&gt;
     artist = newArtist;                               &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 '''- (NSString *)description                           // a'''&lt;br /&gt;
                         '''{'''&lt;br /&gt;
                         '''    return [self name];                             // b'''&lt;br /&gt;
                         '''}'''&lt;br /&gt;
 &lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method that overrides the method by the same name in the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class. We don't need to declare this method in the ''Song.h'' interface file, as it is already part of the interface declared by &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Returns the name of the song as its description, using the special &amp;lt;tt&amp;gt;self&amp;lt;/tt&amp;gt; variable that points to the object under operation. We could have just returned the variable directly from this method, but using the &amp;lt;tt&amp;gt;[self name]&amp;lt;/tt&amp;gt; message means that if the internal implementation of the &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class changes, this method will work correctly with no additional work.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run the program. You should see the following output on the console:&lt;br /&gt;
&lt;br /&gt;
 2002-06-11 22:32:20.435 songs[7096] Song 1: We Have Explosive&lt;br /&gt;
 2002-06-11 22:32:20.436 songs[7096] Song 2: Loops of Fury&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Overriding the &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method allows us to assign much more meaningful strings for output than &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;'s default class name and memory address output.&lt;br /&gt;
&lt;br /&gt;
=== Calling Superclass Methods ===&lt;br /&gt;
&lt;br /&gt;
Sometimes, in a method that overrides a superclass's method, calling the functionality in the superclass's method can be useful. To do this, you can send a message to &amp;lt;tt&amp;gt;super&amp;lt;/tt&amp;gt;, a special variable in the Objective-C language. When you send a message to &amp;lt;tt&amp;gt;super&amp;lt;/tt&amp;gt;, it indicates that an inherited method should be performed, rather than the method in the current class.&lt;br /&gt;
&lt;br /&gt;
For example, if we wanted to print the same information that the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class prints in the &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method, we could implement our &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method as follows:&lt;br /&gt;
&lt;br /&gt;
 - (NSString *)description&lt;br /&gt;
 {&lt;br /&gt;
     return '''[super description];'''&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If we were to make this change, we would see the following output:&lt;br /&gt;
&lt;br /&gt;
 2002-06-11 22:37:03.997 Songs[7115] Song 1: &amp;lt;Song: 0x53100&amp;gt;&lt;br /&gt;
 2002-06-11 22:37:03.998 Songs[7115] Song 2: &amp;lt;Song: 0x530a0&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since this defeats the purpose of overriding the &amp;lt;tt&amp;gt;description&amp;lt;/tt&amp;gt; method, we're not going to add this implementation to our &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class. If you experiment with this, be sure to set it back to &amp;lt;tt&amp;gt;return&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;[self description]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Object Creation ===&lt;br /&gt;
&lt;br /&gt;
One of a class's primary functions is to create new objects of the type defined by the class. As we've seen, objects are created at runtime in a two-step process that first allocates memory for the instance variables of the new object and then initializes those variables. We've said this before, but because it's important, we'll repeat it here: an &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; message should ''always'' be coupled with an &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; message in the same line of code. The receiver for the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; message is a class, while the receiver for the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; message is the new object instance:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;TheClass * newObject = [[TheClass alloc] init];&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method dynamically allocates memory for a new instance of the receiving class and returns the new object. The receiver for the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; message is the new object that was dynamically allocated by &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt;. An object isn't ready for use until it is initialized, but it should be initialized only once. The version of the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method defined in the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class does very little. In fact, it simply returns &amp;lt;tt&amp;gt;self&amp;lt;/tt&amp;gt;, a special variable in Objective-C that is defined to point to the object that is called upon by the method.&lt;br /&gt;
&lt;br /&gt;
After being allocated and initialized, a new object is a fully functional member of its class with its own set of variables. The &amp;lt;tt&amp;gt;newObject&amp;lt;/tt&amp;gt; object can receive messages, store values in its instance variables, and so on.&lt;br /&gt;
&lt;br /&gt;
Subclass versions of the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method should return the new object (&amp;lt;tt&amp;gt;self&amp;lt;/tt&amp;gt;) after it has been successfully initialized. If it can't be initialized, the method should release the object and return &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;. In some cases, an &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method might release the new object and return a substitute. Programs should therefore always use the object returned by &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt;, and not necessarily the one returned by &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Subclass versions of &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; incorporate the initialization code for the classes from which they inherit through a message to &amp;lt;tt&amp;gt;super&amp;lt;/tt&amp;gt;. When working with classes that inherit from &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;, a simple call to the superclass &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method, as shown in the following code block, is sufficient.&lt;br /&gt;
&lt;br /&gt;
 - init&lt;br /&gt;
 {&lt;br /&gt;
     [super init];&lt;br /&gt;
     /* class-specific initialization goes here */&lt;br /&gt;
     return self;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Note that the message to &amp;lt;tt&amp;gt;super&amp;lt;/tt&amp;gt; precedes the initialization code added in the method. This ensures that initialization proceeds in the order of inheritance.&lt;br /&gt;
&lt;br /&gt;
However, since extending classes other than &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; may return a different object than that on which the initializer was called, you must be more careful in these cases and use the following code:&lt;br /&gt;
&lt;br /&gt;
 - init&lt;br /&gt;
 {&lt;br /&gt;
     if (self = [super init]) {&lt;br /&gt;
         /* class specific initialization goes here */&lt;br /&gt;
     }&lt;br /&gt;
     return self;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Note that this code checks to see if &amp;lt;tt&amp;gt;super&amp;lt;/tt&amp;gt; returned an object, or &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;, before doing any initialization itself. This code will work in any situation; however, none of the classes that we create in this book require these checks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
If you have been observant, you may have noticed that we have used two kinds of syntax to denote comments. The first is the traditional &amp;lt;tt&amp;gt;/* . . . */&amp;lt;/tt&amp;gt; C-style comment. The second is the newer &amp;lt;tt&amp;gt;//&amp;lt;/tt&amp;gt; style comment that continues to the end of the line. You'll see both forms used quite frequently in Objective-C code. There really aren't any guidelines as to which style should be used where. You should simply use whichever works best, given the context of the comment.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Designated initializers ====&lt;br /&gt;
&lt;br /&gt;
Subclasses often define initializer methods with additional arguments to allow specific values to be set. The more arguments an initializer method has, the more freedom it gives you to determine the character of initialized objects. Classes often have a set of initializer methods, each with a different number of arguments, to set up objects ahead of time with appropriate information. For example, we could define the following initializer method for our &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class:&lt;br /&gt;
&lt;br /&gt;
 - (id)initWithName:(NSString *)newName artist:(NSString *)newArtist;&lt;br /&gt;
&lt;br /&gt;
This initializer allows us to create new &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; objects and set them up with one line of code rather than three. For this to work properly in cases where users of this class don't call this initializer, but simply use the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method, we make sure that the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method calls this initializer with appropriate default arguments. This method is called the ''designated initializer'' for the class. The other initialization methods defined in this class invoke the designated initializer through messages to &amp;lt;tt&amp;gt;self&amp;lt;/tt&amp;gt;. In this way, all the initializers are chained together. The designated initializer should always call its superclass's designated initializer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
Typically, though not always, the designated initializer is the one with the most arguments. The only way to determine the designated initializer of a class accurately is to read the documentation for the class.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;To work with designated initializers, edit ''Song.h''and add the initializers, as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-8|Example 3-8]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-8&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-8. Adding a designated initializer'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 @interface Song : NSObject {&lt;br /&gt;
     NSString *name;&lt;br /&gt;
     NSString *artist;&lt;br /&gt;
 }&lt;br /&gt;
 '''- (id)initWithName:(NSString *)newName artist:(NSString *)newArtist;'''&lt;br /&gt;
 - (NSString *)name;&lt;br /&gt;
 - (void)setName:(NSString *)newName; &lt;br /&gt;
 - (NSString *)artist;&lt;br /&gt;
 - (void)setArtist:(NSString *)newArtist;&lt;br /&gt;
 &lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added declares an initializer for our &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class that takes the name of the song as well as the artist.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Now add the initializer implementations to ''Song.m''as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-9|Example 3-9]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-9&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-9. Designated initializer implementation'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;quot;Song.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 @implementation Song&lt;br /&gt;
 &lt;br /&gt;
 '''- (id)init                                  // a'''&lt;br /&gt;
                               '''{'''&lt;br /&gt;
                               '''    return [self initWithName:nil artist:nil];'''&lt;br /&gt;
                               '''}'''&lt;br /&gt;
 &lt;br /&gt;
                               '''- (id)initWithName:(NSString *)newName artist:(NSString *)newArtist     // b'''&lt;br /&gt;
                               '''{'''&lt;br /&gt;
                               '''    [super init];                                                       // c'''&lt;br /&gt;
                               '''    [self setName:newName];                                             // d'''&lt;br /&gt;
                               '''    [self setArtist:newArtist];                                         // e'''&lt;br /&gt;
                               '''    return self;                                                        // f'''&lt;br /&gt;
 &lt;br /&gt;
 }&lt;br /&gt;
 - (NSString *)name&lt;br /&gt;
 {&lt;br /&gt;
     return name;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (void)setName:(NSString *)newName&lt;br /&gt;
 {&lt;br /&gt;
     [newName retain];&lt;br /&gt;
     [name release];&lt;br /&gt;
     name = newName;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (NSString *)artist&lt;br /&gt;
 {&lt;br /&gt;
     return artist;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (void)setArtist:(NSString *)newArtist&lt;br /&gt;
 {&lt;br /&gt;
     [newArtist retain];&lt;br /&gt;
     [artist release];&lt;br /&gt;
     artist = newArtist;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (NSString *)description&lt;br /&gt;
 {&lt;br /&gt;
     return [super description];&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-9|Example 3-9]] performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Overrides the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method provided by &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;. This overridden method calls the new designated initializer with &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; string arguments for the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; arguments.&lt;br /&gt;
# Declares our designated initializer with the same signature we used in ''Song.h''.&lt;br /&gt;
# Calls the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method of the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; superclass.&lt;br /&gt;
# Sets the name of the new object.&lt;br /&gt;
# Sets the artist of the new object.&lt;br /&gt;
# Returns the freshly initialized object, ready for use.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Now, edit the ''main.m''file to match [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-10|Example 3-10]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-10&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-10. Using the designated initializer'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;br /&gt;
#import &amp;quot;Song.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;    Song * song1 = [[Song alloc] initWithName:@&amp;quot;We Have Explosive&amp;quot;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                               '''                                       artist:@&amp;quot;The Future Sound of London&amp;quot;];'''&lt;br /&gt;
                               '''&amp;lt;nowiki&amp;gt;    Song * song2 = [[Song alloc] initWithName:@&amp;quot;Loops of Fury&amp;quot;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                               '''                                       artist:@&amp;quot;The Chemical Brothers&amp;quot;];'''&lt;br /&gt;
 &lt;br /&gt;
     NSLog(@&amp;quot;Song 1: %@&amp;quot;, song1);&lt;br /&gt;
     NSLog(@&amp;quot;Song 2: %@&amp;quot;, song2);&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this code, we've simply replaced the longer three lines with our new initializer.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run the program. You should see the following familiar output:&lt;br /&gt;
&lt;br /&gt;
 2002-06-11 23:08:07.783 Songs[7195] Song 1: We Have Explosive&lt;br /&gt;
 2002-06-11 23:08:07.784 Songs[7195] Song 2: Loops of Fury&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
As you can see in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-10|Example 3-10]], a line of code is often too long to fit on one line. Project Builder has autoindentation functionality to make these constructs look good automatically, so you don't have to type in a bunch of spaces manually. Simply go into Project Builder's preferences, select the Indentation pane, and make sure that the &amp;quot;Syntax-aware indenting&amp;quot; checkbox is checked.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Object Deallocation ===&lt;br /&gt;
&lt;br /&gt;
We have a flaw in our very simple program in the form of a memory leak. Of course, this leak is probably not going to hurt anybody — since the program exits so quickly, allowing the operating system to reclaim all the memory belonging to the process — but it doesn't pay to get into bad habits. As well, code has a tendency to be reused in ways that the original author did not expect. Therefore, you should always make a point of cleaning up after your code, no matter how simple it is.&lt;br /&gt;
&lt;br /&gt;
When an object is no longer being used by the program, it must be deallocated. When an object is released, the &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method (provided by the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class) is called, letting it release objects it has created, free allocated memory, and so on. Since our &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class has two instance variable objects, they need to be released when an instance of the class is released.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;To do this, we need to add a &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method implementation to our &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class in the''Song.m'' file, as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-11|Example 3-11]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-11&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-11. Adding a deallocation method'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;quot;Song.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 @implementation Song&lt;br /&gt;
 &lt;br /&gt;
 - (id)init&lt;br /&gt;
 {&lt;br /&gt;
     return [self initWithName:nil artist:nil];&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 - (id)initWithName:(NSString *)newName artist:(NSString *)newArtist&lt;br /&gt;
 {&lt;br /&gt;
     [super init];&lt;br /&gt;
     [self setName:newName];&lt;br /&gt;
     [self setArtist:newArtist];&lt;br /&gt;
     return self;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 '''- (void)dealloc                                          // a'''&lt;br /&gt;
                            '''{'''&lt;br /&gt;
                            '''    NSLog(@&amp;quot;Deallocating %@&amp;quot;, self);                     // b'''&lt;br /&gt;
                            '''    [name release];                                      // c'''&lt;br /&gt;
                            '''    [artist release];                                    // d'''&lt;br /&gt;
                            '''    [super dealloc];                                     // e'''&lt;br /&gt;
                            '''}'''&lt;br /&gt;
 &lt;br /&gt;
 - (NSString *)name&lt;br /&gt;
 {&lt;br /&gt;
     return name;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (void)setName:(NSString *)newName&lt;br /&gt;
 {&lt;br /&gt;
     [newName retain];&lt;br /&gt;
     [name release];&lt;br /&gt;
     name = newName;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (NSString *)artist&lt;br /&gt;
 {&lt;br /&gt;
     return artist;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (void)setArtist:(NSString *)newArtist&lt;br /&gt;
 {&lt;br /&gt;
     [newArtist retain];&lt;br /&gt;
     [artist release];&lt;br /&gt;
     artist = newArtist;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 - (NSString *)description&lt;br /&gt;
 {&lt;br /&gt;
     return [self name];&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code that we added in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-11|Example 3-11]] performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Declares the &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method. Note that since the &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method is defined by the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class, we don't have to declare it in the ''Song.h'' header file.&lt;br /&gt;
# Prints out a message saying that the object is being deallocated.&lt;br /&gt;
# Releases the &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; instance variable.&lt;br /&gt;
# Releases the &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; instance variable.&lt;br /&gt;
# Calls &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; on the superclass, allowing the deallocation functionality of the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class to operate. When you override the default &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; functionality, you must always be sure to call &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; in the superclass.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Edit the ''main.m'' source file with the changes shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C#learncocoa2-CHP-3-EX-12|Example 3-12]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-3-EX-12&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-12. Releasing the song objects created'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;br /&gt;
#import &amp;quot;Song.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
    Song * song1 = [[Song alloc] initWithName:@&amp;quot;We Have Explosive&amp;quot;&lt;br /&gt;
                                       artist:@&amp;quot;The Future Sound of London&amp;quot;];&lt;br /&gt;
    Song * song2 = [[Song alloc] initWithName:@&amp;quot;Loops of Fury&amp;quot;&lt;br /&gt;
                                       artist:@&amp;quot;The Chemical Brothers&amp;quot;];&lt;br /&gt;
&lt;br /&gt;
    NSLog(@&amp;quot;Song 1: %@&amp;quot;, song1);&lt;br /&gt;
    NSLog(@&amp;quot;Song 2: %@&amp;quot;, song2);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''    [song1 release];'''&lt;br /&gt;
                            '''    [song2 release];'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The added code tells the system that we are no longer interested in the &amp;lt;tt&amp;gt;song1&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;song2&amp;lt;/tt&amp;gt; variables. Because we are no longer interested, and there are no other objects interested in these variables, they will be deallocated immediately. This will plug up our memory leak, making it a good citizen.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run the project. You should see the following output:&lt;br /&gt;
&lt;br /&gt;
 2002-06-11 23:12:07.783 songs[7200] Song 1: We Have Explosive&lt;br /&gt;
 2002-06-11 23:12:07.784 songs[7200] Song 2: Loops of Fury&lt;br /&gt;
 2002-06-11 23:12:07.783 songs[7200] Deallocating We Have Explosive&lt;br /&gt;
 2002-06-11 23:12:07.784 songs[7200] Deallocating Loops of Fury&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit|Chapter 4]], we present the finer details of memory management and explain why the act of releasing an object here calls the &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method of our &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; objects.&lt;br /&gt;
&lt;br /&gt;
== Other Concepts ==&lt;br /&gt;
&lt;br /&gt;
There are some other concepts in object-oriented programming and Objective-C that we haven't explored in depth in this chapter. Before you learn too much about these new concepts, you'll want to practice quite a bit with the concepts that you've already learned. We're telling you about these other concepts now so that when you come to them, you won't be surprised.&lt;br /&gt;
&lt;br /&gt;
;Categories&lt;br /&gt;
: You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not to a new class.&lt;br /&gt;
;Protocols&lt;br /&gt;
: Class and category interfaces declare methods that are associated with a particular class—methods that the class implements. Informal and formal protocols, on the other hand, declare methods that are not associated with a class, but which any class—and perhaps many classes—might implement. We'll talk more about protocols in [[Learning Cocoa with Objective-C/Single-Window Applications/Models and Data Functionality|Chapter 9]].&lt;br /&gt;
;Introspection&lt;br /&gt;
: An object, even one typed as &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt;, can reveal its class and divulge other characteristics at runtime. Several introspection methods, such as &amp;lt;tt&amp;gt;isMemberOfClass:&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;isKindOfClass:&amp;lt;/tt&amp;gt;, allow you to ascertain the inheritance relationships of an object and the methods to which it responds.&lt;br /&gt;
&lt;br /&gt;
Remember, you can find out much more information about Objective-C and object-oriented programming in the developer documentation installed on your hard drive along with the Developer Tools (''/Developer/Documentation/Cocoa/ObjectiveC'' ).&lt;br /&gt;
&lt;br /&gt;
== Exercises ==&lt;br /&gt;
&lt;br /&gt;
# Use the resources in [[Learning Cocoa with Objective-C/Appendixes/Using the Foundation and Application Kit API References|Appendix C]], and read the documentation for &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Read the documentation for the &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
# Investigate the &amp;lt;tt&amp;gt;isa&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;self&amp;lt;/tt&amp;gt; variables by having the designated initializer of the Song class print a description of the class.&lt;br /&gt;
== Notes ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	</feed>