What is the difference between an Instance and an Object?

OopObjectInstance

Oop Problem Overview


What is the difference between an Instance and an Object? Is there a difference or not?

Oop Solutions


Solution 1 - Oop

The Instance and Object are from Object Oriented Programming.

For some programming languages like Java, C++, and Smalltalk, it is important to describe and understand code. In other languages that used in Structured Programming, this concept doesn't exist.

This is a view from Structural Programming. There's no real significant difference that should consume too much of your time. There might be some fancy language that some people might take up a lot of spaces to write about, but at the end of the day, as far as a coder, developer, programmer, architect, is concerned, an instance of a class and an object mean the same thing and can often be used interchangeably. I have never met anyone in my career that would be picky and spend a half-hour trying to point out the differences because there's really none. Time can be better spent on other development efforts.

UPDATE With regards to Swift, this is what Apple who invented Swift prefers :

> An instance of a class is traditionally known as an object. However, > Swift classes and structures are much closer in functionality than in > other languages, and much of this chapter describes functionality that > can apply to instances of either a class or a structure type. Because > of this, the more general term instance is used.

Solution 2 - Oop

Excellent question.

I'll explain it in the simplest way possible: Say you have 5 apples in your basket. Each of those apples is an object of type Apple, which has some characteristics (i.e. big, round, grows on trees).

In programming terms, you can have a class called Apple, which has variables size:big, shape:round, habitat:grows on trees. To have 5 apples in your basket, you need to instantiate 5 apples. Apple apple1, Apple apple2, Apple apple3 etc....

Alternatively: Objects are the definitions of something, instances are the physical things.

Does this make sense?

Solution 3 - Oop

Instance: instance means just creating a reference(copy).

object: means when memory location is associated with the object (is a run-time entity of the class) by using the new operator.

In simple words, Instance refers to the copy of the object at a particular time whereas object refers to the memory address of the class.

Solution 4 - Oop

Object:

It is a generice term basically it is a Software bundle that has state(variables) and behaviour(methods)

Class:

A blue print(template) for an object instance-it's a unique object thing for example you create a object two times what does that mean is yo have created two instances

Let me give an example

Class student()
{
   private string firstName;
  public student(string fname)
  {
    firstName=fname;
  }
  Public string GetFirstName()
  {
    return firstName;
  }
}

Object example:

Student s1=new student("Martin"); Student s2=new student("Kumar");

The s1,s2 are having object of class Student

Instance:

s1 and s2 are instances of object student the two are unique.

it can be called as reference also.

basically the s1 and s2 are variables that are assigned an object

Solution 5 - Oop

Let's say you're building some chairs.

The diagram that shows how to build a chair and put it together corresponds to a software class.

Let's say you build five chairs according to the pattern in that diagram. Likewise, you could construct five software objects according to the pattern in a class.

Each chair has a unique number burned into the bottom of the seat to identify each specific chair. Chair 3 is one instance of a chair pattern. Likewise, memory location 3 can contain one instance of a software pattern.

So, an instance (chair 3) is a single unique, specific manifestation of a chair pattern.

Solution 6 - Oop

Objects and instances are mostly same; but there is a very small difference. If Car is a class, 3 Cars are 3 different objects. All of these objects are instances. So these 3 cars are objects from instances of the Car class.

But the word "instance" can mean "structure instance" also. But object is only for classes.

All of the objects are instances. Not all of the instances must be objects. Instances may be "structure instances" or "objects". I hope this makes the difference clear to you.

Solution 7 - Oop

Quick and Simple Answer

  • Class : a specification, blueprint for an object
  • Object : physical presence of the class in memory
  • Instance : an unique copy of the object (same structure, different data)

Solution 8 - Oop

An object is a construct, something static that has certain features and traits, such as properties and methods, it can be anything (a string, a usercontrol, etc)

An instance is a unique copy of that object that you can use and do things with.

Imagine a product like a computer.

THE xw6400 workstation is an object

YOUR xw6400 workstation, (or YOUR WIFE's xw6400 workstation) is an instance of the xw6400 workstation object

Solution 9 - Oop

Once you instantiate a class (using new), that instantiated thing becomes an object. An object is something that can adhere to encapsulation, polymorphism, abstraction principles of object oriented programming and the real thing a program interacts with to consume the instance members defined in class. Object contains instance members (non-static members).

Thus instance of a class is an object. The word ‘instance’ is used when you are referring to the origin from where it born, it's more clearer if you say ‘instance of a class’ compared to ‘object of a class’ (although the latter can be used to).

Can also read the 'Inner classes' section of this java document on nested classes - https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Solution 10 - Oop

Java is an object-oriented programming language (OOP). This means, that everything in Java, except of the primitive types is an object.

Now, Java objects are similar to real-world objects. For example we can create a car object in Java, which will have properties like current speed and color; and behavior like: accelerate and park.

That's Object.

enter image description here

Instance, on the other side, is a uniquely initialized copy of that object that looks like Car car = new Car().

Check it out to learn more about Java classes and object

Solution 11 - Oop

I can't believe, except for one guy no one has used the code to explain this, let me give it a shot too!

// Design Class
class HumanClass {
    var name:String
    init(name:String) {
        self.name = name
    }
}

var humanClassObject1 = HumanClass(name: "Rehan") 

Now the left side i.e: "humanClassObject1" is the object and the right side i.e: HumanClass(name: "Rehan") is the instance of this object.

var humanClassObject2 = HumanClass(name: "Ahmad") // again object on left and it's instance on the right.

So basically, instance contains the specific values for that object and objects contains the memory location (at run-time).

Remember the famous statement "object reference not set to an instance of an object", this means that non-initialised objects don't have any instance. In some programming languages like swift the compiler will not allow you to even design a class that don't have any way to initialise all it's members (variable eg: name, age e.t.c), but in some language you are allowed to do this:

// Design Class
class HumanClass {
    var name:String // See we don't have any way to initialise name property.
}

And the error will only be shown at run time when you try to do something like this:

var myClass = HumanClass()
print(myClass.name) // will give, object reference not set to an instance of the object.

This error indicates that, the specific values (for variables\property) is the "INSTANCE" as i tried to explain this above! And the object i.e: "myClass" contains the memory location (at run-time).

Solution 12 - Oop

This answer may be seen as trite, but worrying about the differences between an instance and object is already trite city.

I think its best depicted in javascript:

let obj= {"poo":1} 
// "obj" is an object

verses

Class Trash {
    constructor(){this.poo = 1;}
}

let i = new Trash();
// "i" is an instance

Solution 13 - Oop

An instance is a specific representation of an object. An object is a generic thing while an instance is a single object that has been created in memory. Usually an instance will have values assigned to it's properties that differentiates it from other instances of the type of object.

Solution 14 - Oop

If we see the Definition of Object and Instance object -

Memory allocated for the member of class at run time is called object or object is the instance of Class.

Let us see the Definition of instance -

Memory allocated For Any at run time is called as instance variable.

Now understand the meaning of any run time memory allocation happen in C also through Malloc, Calloc, Realloc such:

struct p
{

}
p *t1
t1=(p) malloc(sizeof(p))

So here also we are allocating run time memory allocation but here we call as instance so t1 is instance here we can not say t1 as object so Every object is the instance of Class but every Instance is not Object.

Solution 15 - Oop

Object - An instance of a class that has its own state and access to all of the behaviour defined by its class.

Instance - Reference to an memory area for that particular class.

Solution 16 - Oop

Class : A class is a blue print. Object : It is the copy of the class. Instance : Its a variable which is used to hold memory address of the object.

A very basic analytical example

Class House --> Blueprint of the house. But you can't live in the blue print. You need a physical House which is the instance of the class to live in. i.e., actual address of the object is instance. Instances represent objects.

Solution 17 - Oop

When a variable is declared of a custom type (class), only a reference is created, which is called an object. At this stage, no memory is allocated to this object. It acts just as a pointer (to the location where the object will be stored in future). This process is called 'Declaration'.

Employee e; // e is an object

On the other hand, when a variable of custom type is declared using the new operator, which allocates memory in heap to this object and returns the reference to the allocated memory. This object which is now termed as instance. This process is called 'Instantiation'.

Employee e = new Employee(); // e is an instance

However, in some languages such as Java, an object is equivalent to an instance, as evident from the line written in Oracle's documentation on Java:

> Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Solution 18 - Oop

each object said to be an instance of its class but each instance of the class has its own value for each attributes intances shares the attribute name and operation with their intances of class but an object contains an implicit reference to his on class

Solution 19 - Oop

There are 3 things you need to understand : Class , Object and Instance.

Class : Class is the blueprint code from which you will create an Object(s)

Object : When memory is allocated to the data entity (created from blueprint class) , that data entity or reference to it is called Object

Instance : When data is filled in an Object , it becomes an instance of that Object. It can also be called a state of that Object.

Example : In context with C# (objects are reference type here)

Lets say we have a class like this (This is your blueprint code)

public class Animal
{
//some fields and methods     
}

We create an object like this

Animal a = new Animal();
Animal b = a;
Animal c = a;
Animal d = b;

So here is the question : How many objects and instances are here ?

Answer : There is only 1 object but 4 instances.

Why ?

In first line (Animal a = new Animal();),we created an Object from class Animal with new Operator. That Object is somewhere on your RAM. And the reference to that Object is in "a". We have 1 object and 1 instance at this time.

Now in next line, we assign b with a. Here Object is not copied but the reference of object from "a" is stored in "b" too. Thus , we have 2 instances , "a and b".

This goes on and we only copy reference of same object located at some memory.

Finally , we have 4 instances "a,b,c,d" of a single object that was created with new Operator.

(Read how reference type works in C# for more. I hope you understand my language)

Solution 20 - Oop

I can't believe this could be hard to be explain but it actually easier than all the answers I read. It just simple like this.

Firstly, you need understand the definition:

> Instance is a **unique copy-product of an Object.

**unique - have different characteristic but share the same class compare to object

> Object is a name that been used to keep the Class information (i.e > method)

Let say, there is an toy_1 as an object. There is also toy_2 as an object ----> which ALSO an INSTANCE to toy_1. At the same time, toy_1 also an INSTANCE to toy_2. (remember again INSTANCE is a COPY-PRODUCT)

That is why most of the answer I found said it is INTERCHANGABLE. Thank you.

Solution 21 - Oop

I think if we consider other approaches than OOP (mainly by assuming the term Class hasn't always been used, as it's the case for many C projects, which still applied the concept of Objects), following definitions would make the most sense:

A Class defines an interface that objects adhere to.

An Object is an aggregate of different fields. (It doesn't have to "physically" exist, but it can).

All Objects of the same Class can be used in the same way, defined by the Class.

An Instance is a unique realization of an Object.

As many OOP languages use static typing, the Object description is usually part of the Class already. As such, when talking about an Object in C/C++, what usually is meant is the Instance of an Object. In languages that do not have static typing (such as JavaScript), Objects can have different fields, while still sharing the same Class.

Solution 22 - Oop

Regarding the difference between an object and an instance, I do not think there is any consensus.

It looks to me like people change it pretty much interchangeably, in papers, blog posts, books or conversations.

As for me, the way I see it is, an object is a generic and alive entity in the memory, specified by the language it is used in. Just like the Object class in Java. We do not much care its type, or anything else associated with it, whether it is managed by a container or not.

An instance is an object but associated with a type, as in this method accepts Foo instances, or you can not put Animal instances in an instance of a List of Vehicles.

objects for example have locks associated with them, not instances, whereas instances have methods. objects are garbage collected, not instances.

But as I said, this is only how I see it, and I do not think there is any organisation we can refer to for a standard definition between them and everyone will pretty much have their slightly different understanding / definitions (of course within limits).

Solution 23 - Oop

An object is a generic thing, for example, take a linear function in maths

ax+b is an object, While 3x+2 is an instance of that object

Object<<< Instance

General<<< Specific

There is nothing more to this

Solution 24 - Oop

An object can be a class, say you have a class called basketball.

but you want to have multiple basketballs so in your code you create more than 1 basketball

say basketball1 and basketball2. Then you run your application. You now have 2 instances of the object basketball.

Solution 25 - Oop

Object refers to class and instance refers to an object.In other words instance is a copy of an object with particular values in it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstreetparadeView Question on Stackoverflow
Solution 1 - OopKevin Le - KhnleView Answer on Stackoverflow
Solution 2 - OopYuval KarmiView Answer on Stackoverflow
Solution 3 - Oopkarthik reddyView Answer on Stackoverflow
Solution 4 - OopDurai Amuthan.HView Answer on Stackoverflow
Solution 5 - OopBrian ShowalterView Answer on Stackoverflow
Solution 6 - OopDotNet Harsha VardhanView Answer on Stackoverflow
Solution 7 - OopSyed Ekram UddinView Answer on Stackoverflow
Solution 8 - OopGreg OlmsteadView Answer on Stackoverflow
Solution 9 - OopPraveen TiwariView Answer on Stackoverflow
Solution 10 - OopJohnnyView Answer on Stackoverflow
Solution 11 - OopRehan Ali KhanView Answer on Stackoverflow
Solution 12 - OopheugView Answer on Stackoverflow
Solution 13 - OopTLiebeView Answer on Stackoverflow
Solution 14 - OopVivek KumarView Answer on Stackoverflow
Solution 15 - Oopprathipati sView Answer on Stackoverflow
Solution 16 - OopSindhuView Answer on Stackoverflow
Solution 17 - OopSrishtiView Answer on Stackoverflow
Solution 18 - Oopuser2750565View Answer on Stackoverflow
Solution 19 - OopmachinemanView Answer on Stackoverflow
Solution 20 - Oopxk derhakaView Answer on Stackoverflow
Solution 21 - OopThe19thFighterView Answer on Stackoverflow
Solution 22 - OopKoray TugayView Answer on Stackoverflow
Solution 23 - OopChyanit SinghView Answer on Stackoverflow
Solution 24 - OopHexBlitView Answer on Stackoverflow
Solution 25 - OopKaramjit Singh SehdevView Answer on Stackoverflow