Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes object Vs instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object Vs instance" Watch "object Vs instance" New topic
Author

object Vs instance

Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
hai friends,

ive recently attended an interview. there ive faced a question such as this.
what is the difference between an object and instance.

thanking you


shyam kumar
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
I think there is no difference, because object is an instance of the class.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
no i think there is a difference.
have a look


here "a" is the object of class A, but the instance of class B.
Roshan Lal
Ranch Hand

Joined: Nov 13, 2001
Posts: 64
Originally posted by adeel ansari:
no i think there is a difference.
have a look


here "a" is the object of class A, but the instance of class B.




What mentioned above is not quite right. An instance of a subclass is also an instance of its parent class. So the object/instance, variable "a" refers to is both an instance of B and A. However since "a" is given the type "A", it can only access behaviour of "A" from the created instance.
michelle anderson
Greenhorn

Joined: Sep 13, 2004
Posts: 3
I agree with the earlier view that these is no difference. Here is what one O'Reily book ,"Learning Java" says on the subject:

"A class is a blueprint for a part of an application; it lists methods and variables that go into making up that part. Many individual working copies of a given class can exits while an application is active. These individual incarnations are called instances of the class or objects. Two instances of a given class may contain different data, but they always have the same methods."

So, the book seems to be saying that an instance of a class is an object.

Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by michelle anderson:
an instance of a class is an object.


Correct. The term is more generic, though - it typically refers to a concrete manifestation of a more abstract thing. For example, a link between two objects is an instance of the association between the corresponding classes.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The difference may seem subtle, and some of us mix the words when we shouldn't, but the distinction is important. We should all work for precision of language here. As part of design you can consider somewhat separately what classes you should have and how objects interact. Classes have "static" relationships like inheritance and knowledge of each other. Objects have "dynamic" behavior ... inserting a new node in the middle of a doubly linked list has a lot of method calls between objects that happen to be of the same class, very difficult to draw or conceptualize thinking only about the class.

Hope that helps!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hi

There is a good explanation for the above question at the following URL,
TheServerSide.com
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by Abdulla Mamuwala:
There is a good explanation for the above question at the following URL,
TheServerSide.com


With the only drawback being that it's wrong... :roll:

As I explained above, not every instance is an instance of a class. Therefore not every instance is an object.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Daniel, is talking about instances of relationships (and other things that are not classes) common anywhere? I can see it would be useful in the UML meta-model, but it's not something I've ever said out loud.
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by Stan James:
Daniel, is talking about instances of relationships (and other things that are not classes) common anywhere? I can see it would be useful in the UML meta-model, but it's not something I've ever said out loud.


I think what I've seen now and then is talking about a running process as an instance of the executable: "There are three instances of Apache running on this server."

I've also seen it used in the context of methodology: "XP can be seen as an instance of RUP."
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Daniel--I think perhaps you might be invoking several definitions of the same term. Us computer scientists often find ourselves mired in ambiguity due to overloaded terms. As a completely non-CS example, consider the definitions of the term scan. We have both to examine closely as well as to look over or leaf through hastily. Different phases of the development process invoke slightly different meanings all the time...I would posit that the term "instance" as in the case of classes and objects is a different meaning altogether than "instance" in terms of everyday speech, as in "example of".

Having said that, in the context of classes and objects, the terms instance and object are different and non-interchangable. To see this, simply consider the following two statements:

An object is an instance of a class. (makes sense)

An instance is an object of a class. (doesn't make sense)


As far as defining the terms, I'll do my best. A class is a collection of methods and the number and types of data over which those methods can operate. An object is a collection of methods and a set of actual data (the types and number of which are specified by the class of that object) over which those methods operate. The term instance expresses the relationship between such an object and its class.

There are some object-oriented languages that allow for a particular kind of entity called a metaclass. A class has the same kind of relationship with its metaclass as an object has with its class. If you require information about a particular object, you can use the API defined in that object's class. Similarly, if you need information about a particular class, you can use the API defined in that class' metaclass. In these languages, a class is quite literally an instance of its metaclass in the same sense that an object is an instance of its class.

As it turns out, every OO language must grapple with a particular issue inherent in OO theory. To fully realize the theoretical model prescribed by OO theory, every entity must be fixed by a meta-level. That means every object must correspond to a class, every class must correspond to a metaclass, every metaclass must correspond to a meta-metaclass, etc. I've heard of languages that resolve this issue by allowing a metaclass-to be an instance of itself (to define its own API, in other words, just as a class defines the API of an object). Other languages have taken a similar-but-different approach by allowing the metaclass and the meta-metaclass to be instances of each other. All of these approaches ultimately result in some kind of infinite recursion or circular class/metaclass dependency, which is not an elegant solution to the problem but nonetheless brings it to some kind of resolution. (These languages I'm referring to are not hardcore development languages, but rather academic attempts to fully realize the theory of OO.)

Anyway, hope that helps. Hopefully you're not
or or or even worse

sev

[ September 14, 2004: Message edited by: sever oon ]
[ September 14, 2004: Message edited by: sever oon ]
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by sever oon:
I would posit that the term "instance" as in the case of classes and objects is a different meaning altogether than "instance" in terms of everyday speech, as in "example of".


I don't think that those instances (pun intended) of using the term are that different.

from http://whatis.techtarget.com/definition/0,,sid9_gci212355,00.html:

In programming, instantiation is the creation of a real instance or particular realization of an abstraction or template such as a class of objects or a computer process.


A link is the realization of a more abstract association.

A running process is the realization of a more abstract executable on the harddrive.

XP can be seen as a realization of the more abstract process framework RUP.

All of these things have in common that the abstraction defines how something is allowed to behave, but the instance is the thing that actually shows a particular behaviour inside those constrains. The same is true for classes vs. objects.

Does that compute?
[ September 14, 2004: Message edited by: Daniel Mayer ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Roshan Lal:
What mentioned above is not quite right. An instance of a subclass is also an instance of its parent class. So the object/instance, variable "a" refers to is both an instance of B and A. However since "a" is given the type "A", it can only access behaviour of "A" from the created instance.


it can be refer as instance of both but we cant say that "a" is the object of class B. assume this.

Class A a = null;

"a" is an object of class A.

now,

a = new B();

"a" is still an object of class A. but can be said the instance of both.

what you people say? and i think after this much discussion it is no more a beginner question may be some beginner got fever after reading it all.

[ September 15, 2004: Message edited by: adeel ansari ]
[ September 15, 2004: Message edited by: adeel ansari ]
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by adeel ansari:

A a = null;

"a" is an object of class A.

now,

a = new B();

"a" is still an object of class A. but can be said the instance of both.


No, a is not an object at all, it's a reference to an object. The reference is of type A, regardless of the type of the object it's referencing.
Roshan Lal
Ranch Hand

Joined: Nov 13, 2001
Posts: 64
Originally posted by adeel ansari:


it can be refer as instance of both but we cant say that "a" is the object of class B. assume this.

Class A a = null;

"a" is an object of class A.

now,

a = new B();

"a" is still an object of class A. but can be said the instance of both.

>>> NO. This distinction is very important to understand.
"a" is only a variable which may point to an object of type A or subtype of A.


what you people say? and i think after this much discussion it is no more a beginner question may be some beginner got fever after reading it all.

>> Yes, probably should move to intermediate forum.


[ September 15, 2004: Message edited by: adeel ansari ]

[ September 15, 2004: Message edited by: adeel ansari ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
so now you can say out loud that object and instance is somewhat a samething. and your interviewer would be impressed.

but actually what we programmer say usually is,

a is an object of class A, however we know that it is a variable of type A and can hold the reference of class A and reference of any child class of A.

and in everyday life we dont care about the distinction between object and instance. if there is any. but what i am thinking is there should be a difference.

cuz why we say,
instance variable and object diagram

why not,
instance diagram and object variable

or we can ? how about this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object Vs instance
 
Similar Threads
NX: Thoughts on Prototype pattern for DB?
mouse event order in IE
Serialization??
Object of Class N declared inside Class N itself.
Deleting session deletes all my sessions