What is the relationship between a class, object, and instance?
Although this seems like a simple question, I have had many ‘interesting’ discussions and am not exactly sure where one starts and another ends. Here is one possible definition:
A class is created to hold an integer.
This is a class and an object(?), or just a class?
class Person
Property Age as Integer
in code, the instance of an object is created (?), or is this an instance of a class to create an object?
Dim Bob as New Person
I have heard two common definitions, and which one is right - or is this in subjective to someones view?
Possible Definition 1) A class is created in IDE code. When the program runs then the class is considered an object. When a new variable of the object is created, then this is considered an instance of the object. When the class for Bob’s age is changed, then it is the instance of the object data that is changed.
Possible Definition 2) A class is created in IDE code. When the program runs then the class is still considered a class. When a new variable of the object is created, then the instance of the class is created to make an object. When the class for Bob’s age is changed, then it is the object of the class that has changed.
to me… a class and object are for the most part the same thing… it would be code that defines an encapsulated relationship between attributes, properties, events and or methods (any one or combination).
An instance is a variable that can perform the role as defined by the class.
You can have only one definiton of CLASS X, but you can have many INSTANCES of CLASS X
[quote=469525:@Dave S]An instance is a variable that can perform the role as defined by the class.
You can have only one definiton of CLASS X, but you can have many INSTANCES of CLASS X[/quote]
Ok, then the word object and class are possibly the same thing. Let me make another definition to see if I understand it:
Possible Definition 3) A class is created in IDE code. When the program runs then the class is still considered a class, since there can be only one class. When a new variable of the class is created it can be called an instance or an object. When the object (or class) for Bob’s age is changed, then it is the object (or class) that has changed.
It helps to think of these items as real world items.
An object is literally that. The manifestation of anything that can be.
A class is like a template, a mold. As a template, it is not an object.
An instance is the manifestation of a class template. This is an object.
If a class were the blueprint for a coffee mug, the instance would be any individual coffee mug made from the blueprint. Instances - mugs - can have different characteristics of the source template’s defined properties. For example the template would define a spot for the handle, but whether or not the mug has a handle changes from mug to mug - instance to instance.
I find tying the abstract ideas of OOP to a specific IDE makes things more complicated. I don’t like either possible definition you’ve come up with. Edit Redux: Your third definition is close I think, but an age as a class is over engineering. To illustrate, Bob would be an instance of the Human class which would have an Age property.
Something to note, while variables can hold a reference to the instance of an object; the variable itself is not the instance. This is an important differentiation, as otherwise you wouldn’t be able to pass an object around between variables.
There’s very little I’d change about Tim’s excellent answer. I also agree that you can’t really tie these concepts to a specific IDE because they’re almost philosophical concepts.
Here’s my attempt at explaining:
A class: this is the template. The example often given is of a blueprint of a house or a car.
An object: this is where it gets more abstract. An object is something you could make from a class. But a specific one of these is usually called…
An instance: the specific, in-memory actual thing, created from a class. This would be a specific, actual individual house or car.
A lot of languages simply refer to classes and instances.
I think of Class as one of the reserved words used in programming to define a potential object, just like Struct as a reserved word to define a structure.
Object and Instance are more generic/all-encompassing terms, (like the word poly-morphism or inheritance) that are words used in OOP programming in any language.
Having taught object oriented scientific programming for many years, I had significant trouble getting the idea of an object across to my students. Tim Parnell’s list is very useful, but I disagree that a class is not an object. To most it is an object.
But I dont want to start class warfare here, so I respect the template argument, but dont agree with it.
Thanks for the good response Tim, this is quite helpful.
I like your coffee cup manufacturing theme, and the article is also helpful, especially the following quote:
An object is a member or an "instance" of a class.
Using the helpful coffee cup analogy, a blueprint of a coffee cup is a class. When the coffee cup is being manufactured (poured, formed, molded, and coloured) then the coffee cup is being manufactured - or an instance (instantiation?) is occurring. Once the coffee cup is completely finished and and can hold a good cup of coffee, then the object of the class is created, and instantiation is finished.
The steps seem like it starts as a class (blueprint), is instantiated (manufacturing step), and a final object (functional coffee cup) is created. Is an object slightly different than an instance?
My intent is to ask the question and get some type of understanding, and definitely not to create negative feelings.
When I finished my Ph.D., I took an oath to ask questions - even if I looked silly. I would rather be considered a fool for 5 minutes by asking the question, rather than a fool for life by not asking the question at all.
A created object is the result of the instantiation of a class.
Instantiation is the creation of the object. A Cake is an object from the class CakePan, when you make 3 Cakes, you have 3 instances of “CakePan” that also can be said as 3 CakePan objects.
When you do:
Dim cake as CakePan // you can say cake is an CakePan object (holder), but it is “empty” (Nil), it IS NOT an instance of such class yet.
But
cake = New CakePan // cake is an instance of CakePan, the object now has all the proper content.
Most textbooks treat modules and classes as the two most powerful and flexible objects of object oriented programming. I do not understand why a template cannot itself be an object.
But we should all accept the fact that we are dealing with semantics, and that invariably results in point of view positions that are difficult to resolve.
Yes, that is a good point. To make sure I understand it, let me use a house analogy. Blueprints of a house would be the class. There may be two steps for instantiation, meaning the house is built but does not have people living in it. Once the house is populated (filled with furniture and is being used) then the house is now a home (an object).
That’s a good differentiation of the word instance.
in your program when it is designed in the IDE, you create a class. when the program runs there is a part of memory that is the class, you cannot set its properties or run its methods, thats an object. when you ‘copy’ that object into a variable you create an instance of the object.
but that was last century so things may have been altered by time and alcohol…