-3

I am trying to understand the difference between the object and instance and in some forums it is mentioned as same and in other it is mentioned as different.

Q.1 If we instantiate 4 different objects with different properties of a class say

Class A(..)
A a1 = new A(..1..)
A a2 = new A(..2..)
A a3 = new A(..3..)


Then are a1, a2, a3 objects of A or instances of A , or we can say both. And is there a situation where there can be an object of A only or instance of A only ? if no, is there any specific reason why there are two different terms used?

Q2.

Class A(..)
A a1 = new A(..1..)
A a2 = a1


Here as a2 is referencing to a1, now is there any memory difference between a1 and a2.
say class A itself holds 24 bytes of memory
So if we create instance/object(need to confirm) in the second line. We will be having another 24 bytes of memory and additional reference memory for a1 right?. What would be that referenced memory size.
And we have another reference a2, now what will be the memory size for that a2 , is that also 24 bytes + its own referencing size or its just the reference size?


I have written in C# syntax.

supernatural
  • 1,107
  • 11
  • 34
  • 1
    Please do not tag spam. Tag only the language you are programming in. C for example is certainly not relevant as it is not an OO language. – kaylum Aug 29 '21 at 04:54
  • 2
    One question per question. Your second question will definitely depend on the specific language you're asking about. Depending on exactly how you convert that pseudocode into genuine C++ code, you'd get different answers. – Nathan Pierson Aug 29 '21 at 04:56
  • 1
    `Considering any oop language, i have written in C# syntax` Pick a language. There are too many subtleties in dealing with multiple languages. – mjwills Aug 29 '21 at 05:09
  • `we can say both` In c#, they are essentially synonymous. Although it would be more accurate to say they are _variables_ (that are referencing an object). `if no is there any specific reason why there are two different terms used?` Because English is like that. ;) – mjwills Aug 29 '21 at 05:10
  • 1
    `Here as a2 is referencing to a1,` No it isn't. a1 and a2 are both variables, and referencing the same object. They aren't referencing _each other_. – mjwills Aug 29 '21 at 05:11
  • `And we have another reference a2, now what will be the memory size for that a2 , is that also 24 bytes + its own referencing size or its just the reference size?` https://stackoverflow.com/questions/3800882/how-big-is-an-object-reference-in-net – mjwills Aug 29 '21 at 05:13
  • okay, so we have 24 bytes for the class, now for reference variables a1 and a2 considering 64 bit OS, we will having two 64 bit pointers to that object/instance created. Thank you – supernatural Aug 29 '21 at 05:19
  • In colloquial use, the is no real difference between an "object" and an "instance". But if we are being a bit more rigorous, "instance" does not denote a thing. The fundamental is that a "value" is "an instance of" a "type". And an "object" is "an instance of" a "class" ... which is a kind of type. From that you can see that "instance of" is actually a relationship rather than a thing, and "instance" on its own is kind of meaningless. – Stephen C Aug 29 '21 at 06:32
  • I would also note that various people are prepared to jump through hoops to ascribe clear / distinct meanings to "object" and "instance" ... based on all sorts of analogies to real world things, normal (non-IT) English usage of the words and so on. It is (IMO) ultimately futile ... and non-enlightening ... to try and come up with precise definitions for these words. It is like defining "art" or "beauty" or ... "thing". (Leave it to the Philosophers :-) ) – Stephen C Aug 29 '21 at 06:38

1 Answers1

-1

Q1 The object is defined as variables that contain many values.

Lemme give you an abstract example. Let's say there are 10 businessmen teachers who taught some students to be businessmen. All of them are businessmen now, students or teachers. but not all of them were taught. The teachers are passing their knowledge to to the students. So each student is an instance of their respectful teacher.

So basically, a1, a2, and a3 are instances of A. because A passed his values to them. Even though they are all objects which its definition is metioned above. A is the parent.

Also, the syntax you wrote is incorrect.

u need to use var, let, or const. And u need to use these types of brackets:

Class A {
 constructor (){}
}
var a1 = new A{..1..}
var a2 = new A{..2..}
var a3 = new A{..3..}