01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

A whirlwind tour of object-oriented programming concepts<br />

57<br />

3.1.3 What are classes?<br />

A class is a specification, or blueprint, designed to describe the structure of one or<br />

more objects in a system that share a similar purpose. Classes are the most basic form<br />

of encapsulation in <strong>Objective</strong>-C: they combine a small amount of data with a set of relevant<br />

functions to manipulate or interact with that data.<br />

Once a class is defined, its name becomes a new data type, meaning that you can<br />

declare a variable to be of that type. The class is like a factory line, rolling out cookie<br />

after cookie on demand. When instantiated, each newly created object has its own<br />

copy of the data and methods defined by the class. By convention, class names begin<br />

with a capital letter to differentiate them from method and instance variable names,<br />

which typically start with a lowercase letter.<br />

3.1.4 Inheritance and polymorphism<br />

An advantage of object-oriented programming is the ability to reuse existing classes<br />

time and time again. Not only can you create additional objects with little effort, but<br />

one class can build on the foundations of another. This technique, called inheritance, is<br />

similar to a family tree. One class in the system inherits, or derives, from another.<br />

Inheritance has two major benefits:<br />

■<br />

■<br />

Code reuse—A subclass inherits all the data and logic defined by its superclass<br />

(ancestor). This avoids duplicating identical code in the definition of similar<br />

classes.<br />

Specialization—A subclass can append additional data or logic and/or override<br />

existing behavior provided by the superclass.<br />

Class clusters<br />

While researching <strong>Objective</strong>-C you may come across the concept of class clusters.<br />

These are an example of inheritance and polymorphism. In a class cluster, a superclass<br />

is documented, while a number of subclasses are purposely left undocumented<br />

(as private, implementation-specific details).<br />

As an example, virtually all <strong>Objective</strong>-C tutorials, including this book, discuss using<br />

the NSString class to store text. It may surprise you that in most cases there will<br />

never be an object of type NSString created in your application.<br />

Instead, when you request a new NSString object, one of a number of subclasses<br />

with names such as NSCFString is created in its place. Because these subclasses<br />

inherit from NSString, they can be used in its place. As the old saying goes, “If it<br />

walks like a duck, quacks like a duck, and swims like a duck, it’s probably a duck.”<br />

NSString uses these “hidden” subclasses to allow itself to optimize memory and<br />

resource usage based on the specifics of each string generated.<br />

The next time you’re in the Xcode debugger, hover the mouse over a variable of type<br />

NSString. In the data tip that appears, you’ll probably see the object’s data type<br />

listed as NSCFString. This is a class cluster in action.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!