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?
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".
They are the same thing in most object-oriented languages. "Instance of a class" is just how the term "object" is defined.
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.
(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.
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
.
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".
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.
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.
Class
tells you what you want to have (data schema), and how you want to operate on it (methods).- 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.
"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.