views:

265

answers:

10

I'd like to know the difference between object and instance of class. I feel both are same, but why do we call with two names. Can anybody explain with real life example?

+2  A: 

They are practically the same in this context. "Object" may be used as a slightly more general / vague term, "instance" is typically used more specifically as "instance of a class".

Péter Török
+1  A: 

Same thing: an "object" is really "an instance of a class".

jldupont
+6  A: 

They are the same thing in most object-oriented languages. "Instance of a class" is just how the term "object" is defined.

Bill the Lizard
No it isn't. Otherwise javascript and all other class-less languages (lua, etc.) would not have objects. And, they do. See Dario's answer for the most-right answer here.
Evan Carroll
@Evan Carroll: Check the edit history of the question. It had the `java` tag when I answered.
Bill the Lizard
+2  A: 

They are the same. In context, "instance" emphasizes the fact that it is an instantiation of the class definition; "object" emphasizes the fact that it is a unique entity.

Rachel
+5  A: 

(Edit: This question originally had the "java" tag. Thus language without classes were out of scope.)

Object

A pattern (exemplar) of a class. The class Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur.

Instance

One can have an instance of a class; the instance is the actual object created at runtime. In programmer jargon, the Lassie object is an instance of the Dog class. The set of values of the attributes of a particular object is called its state. The object consists of state and the behaviour that's defined in the object's class.

(Wikipedia, Object-oriented programming)

sfussenegger
A: 

I can illustrate this best with python, so here goes: In Python you can create a class. Lets call it the Person class. Then you create an instance of the Person class. Lets call him Bob. So bob = Person(). The thing is, both bob and Person are objects. When you do Person(), you're actually calling the constructor on the Person class object, which gives you an instance object - namely bob.

Sudhir Jonathan
And to add to the fun -- Person the class is also an object. :)
Epsilon Prime
+1  A: 

You use instance when you want to highlight that an object is created from a given type or class.

See this paragraph on Wikipedia:

In a language where each object is created from a class, an object is called an instance of that class. If each object has a type, two objects with the same class would have the same datatype. Creating an instance of a class is sometimes referred to as instantiating the class.

A real-world example of an object would be "my dog", which is an instance of a type (a class) called "dog", which is a subclass of a class "animal".

Danilo Piazzalunga
+1. Everyone else is saying "an object is an instance of a class," which is true; but they are also saying, "so they are the same thing." Not quite. *of a class* tells us that "instance" has relationship connotations that are absent from "object", even though every object *is* an instantiation of one (or, in some languages, more) class(es).
Carl Manaster
Actually that isn't true. Some languages implement Objects without classes.
Evan Carroll
@evan "in a language where each object is created from a class"
sfussenegger
+2  A: 

An object is a more general term. C uses the term object, even though it doesn't have classes at all -- from its viewpoint, any variable (for example) is an object.

A few object oriented languages don't have classes either. A class is a way of specifying the properties of objects; all the objects of a given class share those properties. In some languages (e.g., Javascript, Self), however, you don't have to specify those properties in a class. Rather, start with one object (the "exemplar") from which you clone a new object and modify it as you see fit. When/if you create a number of objects without modifying them, you can end up with a "class" in a manner of speaking (objects with the same properties), but that's more or less an accident.

Jerry Coffin
+4  A: 

Just for the sake of completeness: In prototype-based programming languages, no distinctions between classes and instances/objects are drawn.

Let's step back if you're asking this question chances are you only know of class-based object orientation. It kinda works like this.

  1. Class tells you what you want to have (data schema), and how you want to operate on it (methods).
  2. Create an Object fill it in with what it needs (data), and use the operations defined with it.

However, you don't need a Class to have an object if you can find a different way to answer the question what do you want to my object to do and how. In an alternative model called called prototype object orientation instances are created by cloning existing ones (prototypes) and replicating and extending the behavior of the clone. The objects have no schema to follow in this format. They're just a grouping of data and stuff that acts on the data.

Dario
This is essential to understanding the question and was excluded from the other answers. I think if your answer grew in substance a little bit more you would be the obvious winner. The majority of these answers are wrong or way off. I was right about to post this reference too. You can only instantiate a non-object. It wouldn't make sense to say A is an instance of A and A is an object - yet, we say A is an instance of B and B is class.
Evan Carroll
Feel free to edit
Dario
A: 

"object", "class object" and "instance" terms has only historical distinctions. Currently there is even one article in Wiki for all of this:)

Here additional proofs, that terms "object" and "instance" are quals (from "Object-Oriented Software Construction" by Bertran Meyer):

An object belonging to the set of objects described by an ADT specification is called an instance of the ADT. For example, a specific stack which satisfies the properties of the STACK abstract data type will be an instance of STACK.

Like an ADT, a class is a type: it describes a set of possible data structures, called the instances of the class. Abstract data types too have instances; the difference is that an instance of an ADT is a purely mathematical element (a member of some mathematical set), whereas an instance of a class is a data structure that may be represented in the memory of a computer and manipulated by a software system.

The definition of “class” yields as a byproduct a definition of “object”. An object is simply an instance of some class. For example an instance of class STACK — a data structure representing a particular stack — is an object; so is an instance of class POINT, representing a particular point in two-dimensional space.

Sergey Teplyakov