Difference between object and instance

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).


The word Class comes from Classification (A Category into which something is put), Now we have all heard that a Class is like a Blueprint,but what does this exactly mean ? It means that the Class holds a Description of a particular Category ,(I would like to show the difference between Class , Object and Instance with example using Java and I would request the readers to visualise it like a Story while reading it , and if you are not familiar with java doesn't matter) So let us start with make a Category called HumanBeing , so the Java program will expressed it as follows

class HumanBeing{ 
   /*We will slowly build this category*/ 
}

Now what attributes does a HumanBeing have in general Name,Age,Height,Weight for now let us limit our self to these four attributes, let us add it to our Category

class HumanBeing{
    private String Name;
    private int Age;
    private float Height;
    private float Weight; 

   /*We still need to add methods*/

}

Now every category has a behaviour for example category Dog has a behaviour to bark,fetch,roll etc... , Similarly our category HumanBeing can also have certain behaviour,for example when we ask our HumanBeing what is your name/age/weight/height? It should give us its name/age/weight/height, so in java we do it as follows

class HumanBeing{
    private String Name;
    private int Age;
    private float Height;
    private float Weight;  

    public HumanBeing(String Name,int Age,float Height,float Weight){
       this.Name = Name;
       this.Age  = Age;
       this.Height = Height;
       this.Weight = Weight;
    }

    public String getName(){
      return this.Name;
    }

    public int getAge(){
      return this.age;
    }

    public float getHeight(){
      return this.Height;
    }

    public float getWeight(){
      return this.Weight;
    }
}

Now we have added behaviour to our category HumanBeing,so we can ask for its name ,age ,height ,weight but whom will you ask these details from , because class HumanBeing is just a category , it is a blueprint for example an Architect makes a blueprint on a paper of the building he wants to build , now we cannot go on live in the blueprint(its description of the building) we can only live in the building once it is built So here we need to make a humanbeing from our category which we have described above , so how do we do that in Java

class Birth{
  public static void main(String [] args){
    HumanBeing firstHuman = new HumanBeing("Adam",25,6.2,90);    
  }
}

Now in the above example we have created our first human being with name age height weight , so what exactly is happening in the above code? . We are Instantiating our category HumanBeing i.e An Object of our class is created

Note : Object and Instance are not Synonyms In some cases it seems like Object and Instance are Synonyms but they are not, I will give both cases

Case 1: Object and Instance seems to be Synonyms
Let me elaborate a bit , when we say HumanBeing firstHuman = new HumanBeing("Adam",25,6.2,90); An Object of our category is created on the heap memory and some address is allocated to it , and firstHuman holds a reference to that address, now this Object is An Object of HumanBeing and also An Instance of HumanBeing. Here it seems like Objects and Instance are Synonyms,I will repeat myself they are not synonyms

Let Us Resume our Story , we have created our firstHuman , now we can ask his name,age,height,weight , this is how we do it in Java

class Birth{
  public static void main(String [] args){
    HumanBeing firstHuman = new HumanBeing("Adam",25,6.2,90);
    System.out.println(firstHuman.getName());
    System.out.println(firstHuman.getAge());
    ...
    ...  
  }
}

so we have first human being and lets move feather by give our first human being some qualification ,let's make him a Doctor , so we need a category called Doctor and give our Doctor some behaviour ,so in java we do as follows

class Doctor extends HumanBeing{
   public Doctor(String Name,int Age,float Height,float Weight){
      super(Name,Age,Height,Weight);
   }
   public void doOperation(){
    /* Do some Operation*/ 
   }

   public void doConsultation(){
    /* Do so Consultation*/
   }
}  

Here we have used the concept of Inheritance which is bringing some reusability in the code , Every Doctor will always be a HumanBeing first , so A Doctor will have Name,Age,Weight,Height which will be Inherited from HumanBeing instead of writing it again , note that we have just written a description of a doctor we have not yet created one , so let us create a Doctor in our class Birth

class Birth{
  public static void main(String [] args){
    Doctor firstDoctor = new Doctor("Strange",40,6,80);
    .......
    .......
    /*Assume some method calls , use of behaviour*/
    .......
    .......    
  }
}

Case 2: Object and Instance are not Synonyms
In the above code we can visualise that we are Instantiating our category Doctor and bringing it to life i.e we are simply creating an Object of the category Doctor , As we already know Object are created on Heap Memory and firstDoctor holds a reference to that Object on the heap ;

This particular Object firstDoctor is as follows (please note firstDoctor holds a reference to the object , it is not the object itself)

  1. firstDoctor is An Object of class Doctor And An Instance of A class Doctor
  2. firstDoctor is Not An Object of class HumanBeing But An Instance of class HumanBeing

So a particular Object can be an instance to a particular class but it need not be an object of that given class

Conclusion:

An Object is said to be an Instance of a particular Category if it satisfies all the characteristic of that particular Category

Real world example will be as follows , we are first born as Humans so image us as Object of Human , now when we grow up we take up responsibilities and learn new skills and play different roles in life example Son, brother, a daughter, father ,mother now What are we actually?We can say that we are Objects of Human But Instances of Brother,daughter,...,etc

I hope this helps

Thank You


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.


enter image description here

Objects are things in memory while instances are things that reference to them. In the above pic:

  • std(instance) -> Student Object (right)
  • std1(instance) -> Student Object (left)
  • std2(instance) -> Student Object (left)
  • std3(instance) -> no object (null)

Tags:

Oop