What is the difference between an Instance and an Object? Is there a difference or not?
相关问题
- how to define constructor for Python's new Nam
- Do the Java Integer and Double objects have unnece
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- Poor Use Case of Object.assign() - Simple Example
- NameError: name 'self' is not defined, eve
- Where is the object browser in visual studio C# 20
- Implementation Strategies for Object Orientation
- An object reference is required for the non-static
- Check if the Type of an Object is inherited from a
- When to use Interfaces in PHP
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
Regarding the difference between an
object
and aninstance
, 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 inJava
. 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 anobject
but associated with a type, as inthis method accepts Foo instances
, oryou can not put Animal instances in an instance of a List of Vehicles
.object
s for example havelocks
associated with them, notinstance
s, whereasinstance
s have methods.objects
are garbage collected, notinstance
s.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).
An instance is an object in memory. Basically you create object and instantiate them when you are using them.
Here is a nice writeup on Classes Vs Objects Vs Instances, he is talking Java but it applies to all OO.
http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/
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?
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
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
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