Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between object and instance

Tags:

oop

I know this sort of question has been asked before, but I still feel that the answer is too ambiguous for me (and, by extension, some/most beginners) to grasp.

I have been trying to teach myself broader concepts of programming than procedural and basic OOP. I understand the concrete concepts of OOP (you make a class that has data (members) and functions (methods) and then instantiate that class at run time to actually do stuff, that kind of thing).

I think I have a handle on what a class is (sort of a design blueprint for an instance to be created in its likeness at compile time). But if that's the case, what is an object? I also know that in prototype based languages, this can muck things up even more, but perhaps this is why there needs to be a clear distinction between object and instance in my mind.

Beyond that, I struggle with the concepts of "object" and "instance". A lot of resources that I read (including answers at SO) say that they are largely the same and that the difference is in semantics. Other people say that there is a true conceptual difference between the two.

Can the experts here at SO help a beginner have that "aha" moment to move forward in the world of OOP?

Note: this isn't homework, I don't go to school - however, I think it would help people that are looking for homework help.

like image 830
TCCV Avatar asked Jul 24 '10 00:07

TCCV


People also ask

What is the difference between class object and instance?

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.

What is the difference between instance and object in Python?

Everything in Python is an object such as integers, lists, dictionaries, functions and so on. Every object has a type and the object types are created using classes. Instance is an object that belongs to a class.

What is object or instance in Java?

To recap, Java is a computer programming language that exists on many devices and supported by many developers. An object is the physical manifestation of a class that occupies memory and has data members. An instance is also the physical manifestation of a class that occupies memory and has data members.

Why is an object called an instance?

Answer. A class can create objects of itself with different characteristics and common behaviour. So, we can say that an Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.


2 Answers

A blueprint for a house design is like a class description. All the houses built from that blueprint are objects of that class. A given house is an instance.

like image 182
joe snyder Avatar answered Oct 05 '22 15:10

joe snyder


The truth is that object oriented programming often creates confusion by creating a disconnect between the philosophical side of development and the actual mechanical workings of the computer. I'll try to contrast the two for you:

The basic concept of OOP is this: Class >> Object >> Instance.

The class = the blue print. The Object is an actual thing that is built based on the 'blue print' (like the house). An instance is a virtual copy (but not a real copy) of the object.

The more technical explanation of an 'instance' is that it is a 'memory reference' or a reference variable. This means that an 'instance' is a variable in memory that only has a memory address of an object in it. The object it addresses is the same object the instance is said to be 'an instance of'. If you have many instances of an object, you really just have many variables in difference places in your memory that all have the same exact memory address in it - which are all the address of the same exact object. You can't ever 'change' an instance, although it looks like you can in your code. What you really do when you 'change' an instance is you change the original object directly. Electronically, the processor goes through one extra place in memory (the reference variable/instance) before it changes the data of the original object.

The process is: processor >> memory location of instance >> memory location of original object.

Note that it doesn't matter which instance you use - the end result will always be the same. ALL the instances will continue to maintain the same exact information in their memory locations - the object's memory address - and only the object will change.

The relationship between class and object is a bit more confusing, although philosophically its the easiest to understand (blue print >> house). If the object is actual data that is held somewhere in memory, what is 'class'? It turns out that mechanically the object is an exact copy of the class. So the class is just another variable somewhere else in memory that holds the same exact information that the object does. Note the difference between the relationships:

Object is a copy of the class. Instance is a variable that holds the memory address of the object.

You can also have multiple objects of the same class and then multiple instances of each of those objects. In these cases, each object's set of instances are equivalent in value, but the instances between objects are not. For example:

Let Class A From Class A let Object1, Object2, and Object3.

//Object1 has the same exact value as object2 and object3, but are in different places in memory.

from Object1>> let obj1_Instance1, obj1_Instace2 , obj1_Instance3

//all of these instances are also equivalent in value and in different places in memory. Their values = Object1.MemoryAddress.

etc.

Things get messier when you start introducing types. Here's an example using types from c#:

//assume class Person exists Person john = new Person();

Actually, this code is easier to analyze if you break it down into two parts:

Person john; john = new Person(); 

In technical speak, the first line 'declares a variable of type Person. But what does that mean?? The general explanation is that I now have an empty variable that can only hold a Person object. But wait a minute - its an empty variable! There is nothing in that variables memory location. It turns out that 'types' are mechanically meaningless. Types were originally invented as a way to manage data and nothing else. Even when you declare primitive types such as int, str, chr (w/o initializing it), nothing happens within the computer. This weird syntactical aspect of programming is part of where people get the idea that classes are the blueprint of objects. OOP's have gotten even more confusing with types with delegate types, event handlers, etc. I would try not focus on them too much and just remember that they are all a misnomer. Nothing changes with the variable until its either becomes an object or is set to a memory address of an object.

The second line is also a bit confusing because it does two things at once:

The right side "new Person()" is evaluated first. It creates a new copy of the Person class - that is, it creates a new object.

The left side "john =", is then evaluated after that. It turns john into a reference variable giving it the memory address of the object that was just created on the right side of the same line.

If you want to become a good developer, its important to understand that no computer environment ever works based on philosophic ideals. Computers aren't even that logical - they're really just a big collection of wires that are glued together using basic boolean circuits (mostly NAND and OR).

like image 30
Rambly John Avatar answered Oct 05 '22 17:10

Rambly John