What is the difference between an object and an instance?

I would like to know the difference between an object and an instance of a class. I feel that both of them are the same, but why do we call two names. Can someone explain a real life example?

+5
language-agnostic oop programming-languages
Feb 18 '10 at 16:50
source share
10 answers

This is the same in most object oriented languages. A "class instance" is how the term "object" is defined.

+9
Feb 18 2018-10-18
source share

Just for completeness: prototype-based programming languages do not distinguish between classes and instances / objects.

Let me step back if you ask this question, most likely you only know the orientation of objects based on classes. It kind of works.

  • Class tells you what you want to have (data schema) and how you want to work with it (methods).
  • Create an Object fill it with what it needs (data), and use the operations defined with it.

However, you do not need Class to have an object, if you can find another way to answer the question, what do you want my object to do and how. In an alternative model, called an instance of the object orientation of the prototype, they are created by cloning existing (prototypes) and replicating and expanding the behavior of the clone. In this format, objects do not have a schema. This is just a grouping of data and data that act on the data.

+5
Feb 18 2018-10-18
source share

(Edit: this question originally had a java tag. Thus, a language without classes was not available.)

An object

A sample (example) of a class. The Dog class defines all possible dogs by listing the characteristics and behavior that they may have; Lassie's object is one particular dog, with specific versions of the characteristic. The dog has fur; the girl has brown and white fur.

Instance

You can have an instance of a class; instance is the actual object created at runtime. In a jargon programmer, the Lassie object is an instance of the Dog class. A set of attribute values ​​for a particular object is called its state. An object consists of state and behavior defined in an object class.

( Wikipedia, Object Oriented Programming )

+4
Feb 18 2018-10-18
source share

"object", "class object" and "instance" have only historical differences. There’s even one article for the Wiki :)

Here is additional evidence that the terms “object” and “instance” are quasi (from Bertrand Meyer from Object-Oriented Software Construction):

An object belonging to a plurality of objects described by the ADT specification is called an ADT instance. For example, a specific stack that satisfies the properties of the abstract type STACK will be an instance of STACK.

Like ADT, a class is a type: it describes a set of possible data structures called class instances. Abstract data types also have instances; the difference is that an ADT instance is a purely mathematical element (a member of some mathematical set), while a class instance is a data structure that can be represented in computer memory and controlled by a software system.

The definition of "class" gives a definition of "object" as a by-product. An object is simply an instance of some class . For example, an instance of the STACK class — a data structure representing a particular stack — is an object; so this is an instance of the class POINT representing a specific point in two-dimensional space.

+4
Feb 19 '10 at 12:21
source share

In this context, they are almost identical. An “object” can be used as a somewhat more general / indefinite term; an “instance” is usually used more specifically as a “class instance”.

+2
Feb 18 2018-10-18
source share

They are the same. In the context, “instance” emphasizes the fact that it is an instance of a class definition; "object" emphasizes the fact that it is a unique object.

+2
Feb 18 2018-10-18
source share

An object is a more general term. C uses the term object, although it has no classes at all - from its point of view, any variable (for example) is an object.

Several object-oriented languages ​​also do not have classes. A class is a way of setting properties of objects; all objects of this class share these properties. However, in some languages ​​(e.g. Javascript, Self) you do not need to specify these properties in the class. Rather, start with one object (the "sample") from which you clone a new object and modify it as you wish. When / if you create several objects without modifying them, you can get a "class" in the form of a conversation (objects with the same properties), but this is more or less an accident.

+2
Feb 18 '10 at 18:32
source share

Same thing: an "object" is really an "instance of a class".

+1
Feb 18 2018-10-18
source share

I can best illustrate this in python, so here: In Python you can create a class. Let's call it the Person class. Then you instantiate the Person class. Bob will call him. So bob = Person() . The fact is that the bob and Person objects are objects. When you execute Person() , you are actually calling the constructor on an object of the Person class that gives the instance object, namely bob .

+1
Feb 18 2018-10-18
source share

You use an instance when you want to highlight that an object is being created from a given type or class.

See this paragraph on Wikipedia :

In a language where each object is created from a class, the object is called an instance of this class. If each object has a type, two objects with the same class will have the same data type. Creating an instance of a class is sometimes called an instance of a class.

A real example of an object would be "my dog," which is an instance of a type (class) called a "dog," which is a subclass of the "animal" class.

+1
Feb 18 '10 at 16:59
source share



All Articles