Changes

Jump to: navigation, search

An Overview of Objective-C Object Oriented Programming

12 bytes removed, 19:55, 18 November 2009
no edit summary
Aside from needing to learn about object oriented programming in order to create your own objects, you will also find that working with entities such as strings, arrays and files in Objective-C will also require an understanding of how to work with objects.
Objective-C provides extensive support for developing object-oriented applications. The subject area of object oriented programming is, however, large. It is not an exaggeration to state that entire books have be been dedicated to the subject. As such, a detailed overview of object oriented software development is beyond the scope of this book. Instead, we will introduce the basic concepts involved in object oriented programming and then move on to explaining the concept as it relates to Objective-C application development.
== What is an Object? ==
<google>ADSDAQBOX_FLOW</google>
An object is Objects are a self-contained piece of functionality that can be easily used, and re-used as the building blocks for a software application.
Objects consist of data variables and functions (called ''methods'') that can be accessed and called on the object to perform tasks. These are collectively referred to as ''members''.
Much as a blueprint or architect's drawing defines what an item or a building will look like once it has been constructed, a class defines what an object will look like when it is created. It defines, for example, what the ''methods'' will do and what the ''member'' variables will be.
== Declaring an Objective-C Class Implementation Interface ==
Before an object can be instantiated we first need to define the class 'blueprint' for the object. In this chapter we will create a Bank Account class to demonstrate the basic concepts of Objective-C object oriented programming.
An Objective-C class is defined in terms of an ''interface'' and an ''implementation''. In the interface section of the definition we specify the base class from which the new class is derived and also define the members and methods that the class will contain. The syntax for the implementation interface section of a class is as follows:
<tt>
</tt>
The ''ClassMembers'' section of the implementation interface defines the variables that are to be contained within the class (also referred to as ''instance variables''). These variables are declared in the same way that any other variable would declared in Objective-C.
The ''ClassMethods'' section defines the methods that are available to be called on the class. These are essentially functions specific to the class that perform a particular operation when called upon.
</pre>
The parent class chosen above is the ''NSObject'' class. This is a standard base class provided with the Objective-C Foundation Classes framework and is the class from which most new class classes are derived. By deriving BankAccount from this parent class we inherit a range of additional methods used in creating, managing and destroying instances that we would otherwise have to write ourselves.
Now that we have the outline syntax for our class, the next step is to add some instance variables to it.
== Define Class Methods ==
The methods of a class are essentially code routines that can be called upon to perform specific tasks within the context the of an instance of that class.
Methods come in two different forms, ''class methods'' and ''instance methods''. Class methods operate at the level of the class, such as creating a new instance of a class and are covered in more detail in [[Writing Objective-C Class Methods]]. Instance methods, on the other hand, operate only on the instance of a class (for example performing an arithmetic operation on two instance variables and returning the result). ''Class methods'' are preceded by a plus (+) sign in the declaration and instance methods are preceded by a minus (-) sign. If the method returns a result, the name of method must be preceded by the data type returned enclosed in parentheses. If a method does not return a result, then the method must be declared as ''void''. If data needs to be passed through to the member method (referred to as ''arguments''), the member method name is followed by a colon, the data type in parentheses and a name for the argument. For example, the declaration of a method to set the account number in our example might read as follows:
<tt>-(void) setAccountNumber: (long) y;</tt>
</pre>
For the sake of economy of typing, the above three statements are frquently frequently rolled into a single line of code as follows:
<pre>
<pre>
[account1 release];
</pre>
== Structuring Object-Oriented Objective-C Code ==
Our example is currently contained within a single source file. In practice, the convention is to place the interface and implementation in their own include files that are then ''included'' in the program source file and complication. Generally the interface section is contained within a file called ''ClassName.h'' where ''ClassName'' is the name of the class. In our case, we would create a file called ''BankAccount.h'' containing the following:
<pre>

Navigation menu