1

I thought I had a pretty good idea of what an object is.. but I was wrong. Could anyone explain what an object is? Or how I should think of it when programming? Please help me understand.

I know it's not the pointer .. so what exactly is the object in a line of code ..

5 Answers 5

4

Conceptually in OOP, an object is a certain instance of a class. A class defines the information and actions for a certain type of object. The quintessential example is of a Car class, that maybe holds a "colour" property and can "drive" or "park". These define what a Car is. Objects are instances of that. There are millions of cars in the world, but only one definition of Car

Now, that is the general OOP view. Objective-C has a rather pure OOP model, based on the concept of message sending. Message sending is performed as so: [obj message]. Essentially, an object in Obj-C is anything that responds to a message. This means that even classes are objects. If you want a more detailed description, this blog post of mine should help: http://pilky.me/view/21

Sign up to request clarification or add additional context in comments.

1 Comment

I guess a class can respond to a message sent to a static method? I don't know what they are called in ObjC but is there another case besides that a class can respond to a message?
2

An object is an instance of a class. So, suppose you have a class person. You might initialize him somewhere:

 Person *p = [[Person alloc] init];

p is a pointer to a person object in memory. The object itself lives in the sizeof(Person) chunk of ram which alloc created.

Comments

1

Basically objects are data plus functions grouped. So you have state (data aka fields, variables) and behavior (functions). The pointer is how you reference the object (in order to use it later, maybe to retrieve its data, execute a function or send a message).

Here you have a nice and simple explanation: http://gnustep.made-it.com/BG-objc/#AEN281

2 Comments

Really, a class is data and functions grouped. The class definition states how the data is to be grouped, but the object is specifically the bits which are in ram. Static fields, for example, are part of the class, but not the object.
I was taking into account that user is a beginner. But sure you are right!
0

Check out this tutorial about the basics of Objective-C:

http://cocoadevcentral.com/d/learn_objectivec/

If you still have questions, feel free to ask.

1 Comment

This one is an EXCELLENT "cheat" sheet. Even if you've been coding for a while, it's worth skimming as it may help explain some things you've been typing, though not truly understanding.
0

I'll start by saying, having used a few other programming languages, the concept of an object in Objective-C is pretty much the same as it is in other Object Oriented Programming languages. There's a good writeup on Wikipedia.

I'd say an easy way to think about it is that a "class" is a blueprint. It describes how a thing should work. An "object" is an actual instance of a thing that follows that blueprint.

You are building a house (program). You need to drive nails. You create an instance of a hammer "object" following the blueprint described by the "class" definition. Now you can hammer nails.

A class generally has an interface and an implementation. This allows you to (and others) to call the code on your object having only to (generally) look at the interface.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.