Objective-C Programming/concepts

From Wikibooks, open books for an open world
< Objective-C Programming(Redirected from Programming:Objective-C concepts)
Jump to: navigation, search

There are a number of key Objective-C concepts that have close relations to the practice of object-oriented programming in general. For the moment, we won't look at exact syntax, until later.

If you have previous experience in object-oriented programming, you may wish to skip these sections and examine the Summary section

Contents

[edit] What is object-oriented programming?

Object-oriented programming (often just shortened to OO or OOP), is a programming paradigm where code is written in units called classes. For example, ANSI C has data in structs, and similarly, an OO language stores data in classes or objects.

For example, if you are creating a word processor, you might represent the document by an object that has text as its data, and associate with it different tasks to perform upon the document's data, such as "spell check".

The word processor then would be a small program that might draw the window and show the document's text. A spell check button would perform the "spell check" task upon that document's object.

However, if we were editing two documents at once, they would differ probably in the contents of the documents- the data. But all the documents would be more or less the same: Each document contains a space for its text, and has a couple of defined tasks that operate on the text. Each document object has essentially the same form. We call this form the class of the object, and we often give it a name. Let's call the document class Document (Style note: we often capitalize names of classes).

Now, each document object has the form of the Document. We say that each document object is an instance of Document. So, for example, our word processor operates on instances of the Document class. Sometimes we may call an instance of the Document class just "a Document".

Remember that each object has some data. For example, with the Document class, each Document has data to hold the text of the document. We call the data that each object holds instance variables. So, a Document may specify an instance variable to hold the text, or an instance variable to hold the word count, or the author, or any other data we may want to specify. Each Document has instance variables that are separate from the instance variables of any other Document that may exist.

Each object also has a number of tasks that it can perform on its instance variables. In Objective-C, we call these tasks methods. The object's methods operate on the object's instance variables.

However, there's more to objects than just this. With Objective-C, we can do a lot of interesting things with objects.

[edit] Working with objects

Let's stick with the example of our word processor. Say we want to create a new instance of Document so we can work with it. This process is called instantiation.

Now that we have an active instance of an object, we can work with it, by using the object's methods. We'll look at defining and working with methods later. Each method merely acts like a function in C, and in Objective-C, there is only a minor syntax change.

When we're completely done with the object and want to get rid of it (so it stops taking up memory), we do what is known as destruction or deallocation of the object. This is like free'ing a malloc'd array, for example, in C. However, if, in the process of using the object's methods, we allocate some extra memory for its instance variables, we must free the memory we allocated also.

[edit] Object techniques

When we're creating new classes, we may want to base the new object's behaviour on a class we have already defined before. For example, if we want to create a document class that has details of style information, we may want to base that class on the `Document` class we already have.

This is a common enough feature that it is included in object-oriented systems, and it is given the name of inheritance. When a class inherits from another, the new class takes on the form of the old class and can add extra instance variables and methods.

So, if we wanted to create a `StyledDocument` class, we would inherit from the `Document` class. In Objective-C, a class only inherits from one other class. This is known as single inheritance.

[edit] Interfaces and implementation

As in the real world, there is a separation between how objects are used, and how objects work. For example, with our word processor, there is a consistent interface to the spellchecker (how it is used), such as a button to jump to the next misspelt word, and a button to go back, and a button to correct it, for example. But, behind the scenes, the way the spellchecker works (the implementation) can be done in many different ways.

In Objective-C, the interface and implementation are kept separate. This allows different objects to work and link together in a well-defined manner. To change channels on your television, for example, you use the interface of the buttons on the television set instead of opening it and fiddling around with the electronics.

Keeping the implementation separate from the interface means that we can interchange anything in the implementation (even substituting it with another implementation) and everything should still work, because objects should interact with each other only through their interfaces.

[edit] Summary

So, before we get to learning Objective-C syntax, let's recap:

An object is an instance of a class
which has defined upon it tasks known as methods
and data known as instance variables
A class can inherit from one (and only one) other class (single inheritance)
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export