Instance of an object and the object itself.
Page 1 of 1
Rayne01




Posts: 39

PostPosted: Thu, 5th Jun 2014 15:59    Post subject: Instance of an object and the object itself.
I just want to clarify my understanding here.

say we have a class Human.

Human H1 = new Human();

H1 is the instance that refers to that particular Human object.

Is the object Human before H1 or new Human();
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Thu, 5th Jun 2014 16:09    Post subject:
Human is a class.

H1 is a fresh object of type Human.

Human H1 says that you are going to define an object called H1, that is of type Human. Then you define it with the = new Human(); So it will go through the constructor of your class and initialize a fresh human with whatever is defined in there.


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Rayne01




Posts: 39

PostPosted: Thu, 5th Jun 2014 16:17    Post subject:
so, essentially:

if we had a constructor of (string name, int age)

Human (type) H1 (object) = new Human ("Rayne", 30); that is a particular instance of an object of type Human.

Without the name and age inserted its simply an object, once the information has been set, the object then becomes an instance of the object of type Human?
Back to top
MinderMast




Posts: 6172

PostPosted: Thu, 5th Jun 2014 16:21    Post subject:
I guess the somewhat mixed up terminologies are at fault for the confusion...

You don't normally refer to an instance of an object, but to an instance of a class. An instance of a class is what you would call an object.
As Pumpy said:
Human - Class (sometimes also referred to as a type)
H1 - instance of a class (object)

An object is a specific thing (as opposed to a type or a class), in simple terms, so an instance of an object and an object would be the same thing (or perhaps a copy of an object?).

Of course if you want to get more confused there is also a class called Object which is aliased by a keyword "object" (in .NET at least) Very Happy

EDIT: Adding to your second post...
Until the constructor is executed and the object is instantiated, H1 is simply a reference to an object of type Human that leads "nowhere". Technically it's the "new Human()" part (with parameters or not) that is an instance (or an object) - until that happens, no instances or objects exist. Only types and references.
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 5th Jun 2014 16:40    Post subject:
An object is an instance of a class.

You don't "have" or "use" an instance of anything in code. You create instances of classes, which you store in objects - the latter is what you use and manipulate Wink

To keep it a bit simpler though, objects and instances are pretty much the same thing. Even in JS, where you can have a "plain object" (var obj = {}) you technically still create an instance of class Object and store it in your object. You use instances via the objects you stored them in.
Back to top
MinderMast




Posts: 6172

PostPosted: Thu, 5th Jun 2014 17:00    Post subject:
In my mind objects and instances are the same thing. An instance of anything could be called an object... if not, what exactly is an instance? Smile

I am not familiar with that particular semantical nuance Very Happy
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 5th Jun 2014 18:58    Post subject:
That's the thing, an instance isn't anything Smile

My first sentence makes that abundantly clear; we all use the word "instance" the same way, but semantically speaking it's incorrect Razz

You can't swap any of those 3 words around and still have that sentence make sense. An instance isn't an object of a class. Nor is a class an instance of an object. Or an instance a class of an object. And so on Razz
Back to top
MinderMast




Posts: 6172

PostPosted: Thu, 5th Jun 2014 19:33    Post subject:
I guess when one speaks of an instance in terms of OOP, it's always implied to be an instance of some class, not an instance as some standalone construct, so I don't think it's incorrect to do so even from a semantic point of view.
I mean, whenever you say you are using an instance (although I doubt anyone often uses that word in a conversation Very Happy) it is implied to be an instance of something.

This is a rather pointless debate though Laughing
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 5th Jun 2014 19:48    Post subject:
Quote:
This is a rather pointless debate though Laughing

Yes

But:
MinderMast wrote:
I guess when one speaks of an instance in terms of OOP, it's always implied to be an instance of some class, not an instance as some standalone construct, so I don't think it's incorrect to do so even from a semantic point of view.
I mean, whenever you say you are using an instance (although I doubt anyone often uses that word in a conversation Very Happy) it is implied to be an instance of something.

That's exactly what I'm saying. We say we're using an instance but what we mean is an object that is an instance of a class Razz
Back to top
WhiteBarbarian




Posts: 6009
Location: Russia
PostPosted: Thu, 5th Jun 2014 20:39    Post subject:
You are going to scare OP off. After reading all that he may "Meh, fuck it all. Will better work at McDonald's" Sad


Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 5th Jun 2014 20:50    Post subject:
Haha Very Happy

Well the short answer to his question is just that from a practical point of view, an object and instance are the same thing.
Back to top
MinderMast




Posts: 6172

PostPosted: Thu, 5th Jun 2014 21:27    Post subject:
Looking at the first post, I think that the problem here was that some sources, confusingly enough, seem to refer to what one would call a class as an object. So when you put all of that together, you seemingly have a class, an object and an instance of that object. However you either have to drop the "instance of an object" or the "class" from the list.

Reality is, there are only two - a type and an instance of that type (AKA an object).

So going by the OP's example, in general terms:
- Human is a type
- H1 is a reference/pointer to an instance of type Human
- new Human() is what actually creates an instance of type Human
- H1= new Human() means that an instance of type Human was created and "assigned" to a pointer H1 - from that point on you could refer to H1 as an instance of type Human

Hope this cleared things up Very Happy
Back to top
Rayne01




Posts: 39

PostPosted: Thu, 5th Jun 2014 23:54    Post subject:
The above was very well presented and gave me a clear indication of my problem, so, in short, an instance of type and an object of type is one in the same thing (instance of an object), essentially?
Back to top
MinderMast




Posts: 6172

PostPosted: Fri, 6th Jun 2014 00:35    Post subject:
Basically, yes, but I barely (if ever) hear anyone call it an "instance of an object", since it kinda clashes with a common understanding of what is an object (which is an instance of some type). It sounds like an instance of an instance, which doesn't make much sense.
It would be best to consider "instance of an object" a misnomer in this context and forget about it Very Happy

This would leave you with an instance of type == an object of type.

In OOP, the most common terms for it are a class and an instance of a class.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group