Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Introduction to Objective-C From Structs to Objects

Shaun Kelly
Shaun Kelly
5,648 Points

Define and explain: object

can somebody explain what an object is ?

5 Answers

An object in Object Oriented Programming is what you can create from a class.

Imagine a video game with zombies you have to fight in order to win. Let's say there are hundreds of zombies. Will you program each of them individually? No, of course not. Instead, you'll write a Zombie class which is basically a recipe for creating a zombie. You will give your zombie class properties like height, strength, etc... and possible actions (known as methods) such as Walk, Attack, etc.

Now, if you need to create a zombie in your app, you can do it with a simple line of code that says create a new zombie as per defined by my Zombie recipe (or to use the correct word, class). The newly created zombie? That's your object.

To resume, objects are concrete stuff created from a recipe known as class.

When you bake cookies, you read a recipe. Each individual cookie is an object and your recipe is the class.

Does that make sense?

A great example! Good work! :-)

ruo pu koh
ruo pu koh
2,669 Points

That makes much more sense then the sphere this and sphere that!!

Hi Shaun,

Entire books can be written on that subject! There's a quick answer but lots of exceptions and variants.

An object is a thing that an application can do something with or use in some way. They can also be called instances. Bear with me ...

There are 'Classes' in some (most?) computer languages. A class is a template that can be used to create multiple instances of the template. That instance is an object. Each object is formed from the same template but that doesn't mean they are the same. The class can have properties that each object inherits. Each object can have different settings of these properties.

Take a class for a car, for example. A car class can have thousands of different properties but let's keep it simple! How about properties just like year registered, make, model name, engine size and colour.

So each instance of a car has these properties that can be set. This makes each object independent of any other objects of the car class but with a common set of properties that the app can use.

1984 Vauxhall Astra 1.8 in green

2010 Mazda RX-8 2.4 in blue

1975 Hillman Imp 800cc in yellow

(all cars I have owned, sadly!)

An object is a piece of data held in memory defined from a class, and inheriting that class's properties.

I'll let someone else explain that more clearly - I found it tricky! Sorry!

Steve.

Shaun Kelly
Shaun Kelly
5,648 Points

haha thats great I love that example nicoschuele. So lets say if I have an app and I have a class about spaceships with properties such as colour, how many weapons and size about that spaceship. In my main.m class, if I call the spaceship class to create a spaceship. The spaceship would be my object, correct ?

Yes! Spot on. Your Spaceship class can create lots of spaceships, called whatever you like - they are your objects and they have colour, weapons and size depending on what you want them to have, so they can all be different.

Spaceship would be your class. You define the properties such as colour and size but you don't assign any values to these properties in your class.

Now, you make an object (I don't know the ObjC syntax so bear with me):

USSEnterprise = new Spaceship
USSEnterprise.colour = "grey"
USSEnterprise.size = "big ass ship"

...and boom, USSEnterprise is an object of type Spaceship.

Makes sense?

It's useful because each time you need to create a new spaceship, you can just build one from your Spaceship class.

Shaun Kelly
Shaun Kelly
5,648 Points

okay thats great thanks so much!. Can you tell me what a definition of "class" is and also "instance"?

The definition of Class, as said previously, is the recipe or the blueprint necessary to create an object. An instance is a synonym for object (in the context of an instance of a class).

Shaun Kelly
Shaun Kelly
5,648 Points

So if you had another spaceship like a Romulan ship made from the same spaceship class. the Romulan spaceship would be an synonym object meaning it's from the same class ?

I meant the words instance and object are synonyms as they are used interchangeably by OO programmers.