tags:

views:

151

answers:

8

Hello Everyone,

I know this sort of question has been asked before, but I still feel that the answer is too ambiguous for me (and, by extension, some/most beginners) to grasp.

I have been trying to teach myself broader concepts of programming than procedural and basic OOP. I understand the concrete concepts of OOP (you make a class that has data (members) and functions (methods) and then instantiate that class at run time to actually do stuff, that kind of thing).

I think I have a handle on what a class is (sort of a design blueprint for an instance to be created in its likeness at compile time). But if that's the case, what is an object? I also know that in prototype based languages, this can muck things up even more, but perhaps this is why there needs to be a clear distinction between object and instance in my mind.

Beyond that, I struggle with the concepts of "object" and "instance". A lot of resources that I read (including answers at SO) say that they are largely the same and that the difference is in semantics. Other people say that there is a true conceptual difference between the two.

Can the experts here at SO help a beginner have that "aha" moment to move forward in the world of OOP?

Thanks again.

Note: this isn't homework, I don't go to school - however, I think it would help people that are looking for homework help.

+1  A: 

An object is an instance of a class (for class based languages).

I think this is the simplest explanation I can come up with.

sukru
I agree that your explanation is about right with the way I have been reading, but there are a lot of entries online that make a distinction between the two. I don't know if this kind of question can't be answered without a language in mind (wow a meta-question, this question is a class that can't be used without instantiating it with a language, lol).
Tim
A: 

I think that it is important to point out that there are generally two things. The blueprint and the copies. People tend to name these different things; classes, objects, instances are just some of the names that people use for them. The important thing is that there is the blueprint and copies of it - regardless of the names for them. If you already have the understanding for these two, just avoid the other things that are confusing you.

Simon
+10  A: 

A blueprint for a house design is like a class description. All the houses built from that blueprint are objects of that class. A given house is an instance.

joe snyder
oooh, that's a good analogy... I think that may have done it!
Tim
Also, there's a street from my hometown that has a little bit of local fame because there are 8 identically built brick houses in a row (before the concept of "housing development" came into being). So I can visualize that very nicely. "Class" is what the architect drew up. The finished houses are "objects" because they're all identically built. Each house is an "instance" because they have (and can continue to be) all treated differently since then [now one has siding :o( ].
Tim
And Static Methods are like the utilities that all the houses share.
WOPR
And obviously those carpenter ants are bugs. HEY! someone get Bugzilla on the phone!
Tim
A: 

Lets compare apples to apples. We all know what an apple is. What it looks like. What it tastes like. That is a class. It is the definition of a thing. It is what we know about a thing.

Now go find an apple. That is an instance. We can see it. We can taste it. We can do things with it. It is what we have.

Class = What we know about something. A definition.

Object/Instance = Something that fits that definition that we have and can do things with.

Jack
+2  A: 

A class defines an object. You can go even further in many languages and say an interface defines common attributes and methods between objects.

An object is something that can represent something in the real world. When you want the object to actually represent something in the real world that object must be instantiated. Instantiation means you must define the characteristics (attributes) of this specific object, usually through a constructor.

Once you have defined these characteristics you now have an instance of an object.

Hope this clears things up.

Derek Litz
I think that's basically saying what Joe said. Correct me if I'm wrong.
Tim
Above Joe says all houses built from the blueprint are objects. True. And he says a given house is an instance. True. But, all the houses are instances and a given house is an object as well. He was making a difference where there was none. In code it is necessary to differentiate between an object and an instance at the moment we construct it. We say it is instantiated when it's actually been built. A common question might be: Did you remember to instantiate the object? ie. if you forgot to call the constructor after you declared an object.
Derek Litz
ah, that's a good distinction. So to say it another way, an instance is an object that has been constructed (ie, had the constructor called).
Tim
A: 

In some cases, the term "object" may be used to describe an instance, but in other cases it's used to describe a reference to an instance. The term "instance" only refers to the actual instance.

For example, a List may be described as a collection of objects, but what it actually holds are references to object instances.

supercat
So in this case, a reference is just a place-holder that contains a link to the instance? Where a link could be something like a memory address?
Tim
+2  A: 

alt text

Objects are things in memory while instances are things that reference to them. In the above pic: std(instance) -> Student Object (right)

std1(instance) -> Student Object (left)

std2(instance) -> Student Object (left)

std3(instance) -> no object (null)

Truong Ha
That is great. I really like the fact that it gets low level. I am going back and forth on whether I like this better than the analogy. I think knowing the 2 together is what I *really* needed. upvote, but not sure if I can give the answer to you because it's already been given to Joe. Thank you, though. This clears up a lot.
Tim
A: 

I have always liked the idea that equals the definition of a class as that of an "Abstract Data Type". That is, when you defined a class you're are defining a new type of "something", his data type representation, in terms of primitives and other "somethings", and his behavior in terms of functions and/or methods. (Sorry for the generality and formalism)

Whenever you defined a class you open a new possibility for defining certain entities with its properties and behavior, when you instantiate and/or create a particular object out of it you're actually materializing that possibility.

Sometimes the terms object and instances are interchangeable. Some OOP purists will maintain that everything is an object, I will not complain, but in the real OOP world, we developers use two concepts:

  1. Class: Abstract Data Type sample from which you can derive other ADT and create objects.
  2. Objects: Also called instances, represents particular examples of the data structures and functions represented by a given Abstract Data Type.
StudiousJoseph