P. 1
The Objective-C Programming Language - Book

The Objective-C Programming Language - Book

3.0

|Views: 689|Likes:
Published by Nikunj Soni

More info:

Published by: Nikunj Soni on Dec 01, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less

06/26/2013

Unrelated code

The class that
declares the
instance variable

A class that
inherits the
instance variable

@private

@protected

@public

A directive applies to all the instance variables listed after it, up to the next directive or the end of the list. In
the following example, theage andevaluation instance variables are private,name,job, andwage are
protected, andboss is public.

@interface Worker : NSObject
{

char *name;

@private

int age;
char *evaluation;

@protected
id job;
float wage;

Class Implementation

41

2009-10-19 | © 2009 Apple Inc. All Rights Reserved.

CHAPTER2

Defining a Class

@public

id boss;

}

By default, all unmarked instance variables (likename above) are@protected.

All instance variables that a class declares, no matter how they’re marked, are within the scope of the class
definition. For example, a class that declares ajob instance variable, such as the Worker class shown above,
can refer to it in a method definition:

- promoteTo:newPosition
{

id old = job;
job = newPosition;
return old;

}

Obviously, if a class couldn’t access its own instance variables, the instance variables would be of no use
whatsoever.

Normally,aclassalsohasaccesstotheinstancevariablesitinherits.Theabilitytorefertoaninstancevariable
is usually inherited along with the variable. It makes sense for classes to have their entire data structures
withintheirscope,especiallyifyouthinkofaclassdefinitionasmerelyanelaborationoftheclassesitinherits
from. ThepromoteTo: method illustrated earlier could just as well have been defined in any class that
inherits thejob instance variable from the Worker class.

However, there are reasons why you might want to restrict inheriting classes from directly accessing an
instance variable:

■ Onceasubclassaccessesaninheritedinstancevariable,theclassthatdeclaresthevariableistiedtothat

partofitsimplementation.Inlaterversions,itcan’teliminatethevariableoraltertheroleitplayswithout
inadvertently breaking the subclass.

■ Moreover, if a subclass accesses an inherited instance variable and alters its value, it may inadvertently

introducebugsintheclassthatdeclaresthevariable,especiallyifthevariableisinvolvedinclass-internal
dependencies.

To limit an instance variable’s scope to just the class that declares it, you must mark it@private. Instance
variables marked@privateare only available to subclasses by calling public accessor methods, if they exist.

Attheotherextreme,markingavariable@publicmakesitgenerallyavailable,evenoutsideofclassdefinitions
that inherit or declare the variable. Normally, to get information stored in an instance variable, other objects
must send a message requesting it. However, a public instance variable can be accessed anywhere as if it
were a field in a C structure. For example:

Worker *ceo = [[Worker alloc] init];
ceo->boss = nil;

Note that the object must be statically typed.

Marking instance variables@public defeats the ability of an object to hide its data. It runs counter to a
fundamental principle of object-oriented programming—the encapsulation of data within objects where
it’s protected from view and inadvertent error. Public instance variables should therefore be avoided except
in extraordinary cases.

42

Class Implementation

2009-10-19 | © 2009 Apple Inc. All Rights Reserved.

CHAPTER2

Defining a Class

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->