Difference between Object and Instance

14

In object-oriented programming, a lot is talked about both instances of classes and objects. Looking for the meaning of each one, I realized that sometimes the concepts are unified and they are treated as synonyms, sometimes the concepts differ and they are treated differently.

Wikipedia itself shows this confusion: in the definition of instance meant that it is " an object whose behavior and state are defined by the class "; already in the definition of object brings that " if an instance of a class ". Is the instance an object and the object an instance? So it would be the same thing?

In an book about orientation to objects there is the definition for object:

  

Object (also known as an instance of an object or instance) is an individual instance of the data structure defined by the class.

What implies that object and instance concepts are the same.

In turn, in this answer about the meaning of "object oriented", it is quoted:

  

The object is an instance of the class and the instance accesses the predefined behaviors.

By implying that the object is an instance, but the concepts are not equivalent, since it would be the instance responsible for accessing the behaviors of the class.

So, in the end, is instance and object synonyms or are they different things that are directly related to one another to the extent that they are commonly considered equivalent?

Other references:

  • a a In OOP an instance of a class is called an object . li>
  • #.

  • #
  • # [...] objects are instantiated from a class, and each object is referred to as an instance of the class ".
asked by anonymous 23.03.2017 / 19:23

3 answers

5

Your reference 3 is a super accurate and correct definition:

  

"An object is an instance of an object type" .

It is soon seen that instance is the technical thing that allows the existence of an object in memory. The word Object, in turn, can be used at a higher level of abstraction.

See: the word "object" exists because the concept of object orientation is to represent in the software the characteristics and behaviors of the real objects of the domain.

So when modeling a billing system, notes and their items are objects ; and when you code the system, classes will be declared to define these objects, and for these objects to exist in memory the instances of these classes will be created.

class NotaFiscal; // não é objeto nem instância, é a definição de um objeto.
var notaFiscal = new NotaFiscal; // "notaFiscal" referencia uma instância
notaFiscal.setNumero(1234);      // a instância representa a Nota Fiscal 1234, 
                                 // que é um objeto de domínio 
                                 // e existe como objeto para além do código fonte.

After the objects are persisted the instances are removed from memory and objects as we know them in the real world will be safe in the database until we need to deal with them again - and then again we will instantiate their classes to be able to look to these beautiful objects and manipulate them.

And if objects are persisted in an object-oriented database, objects continue to exist in the technical realm beyond conceptual even after their instances no longer exist in memory.

Finally, instance and object are interchangeable terms , as in this hypothetical but correct dialog:

  

_ I retrieved all invoices from the database and got this array of 1000 objects.

     

_ It would be better not to have as many instances at once in memory since otherwise the system will slow down, since Nota Fiscal is a large object type.

     Okay, so I'm going to make a paging to just keep 10 objects at a time in the array.

    
24.03.2017 / 21:58
8

You have a class A that can be instantiated n times, generating a group of n Class A objects. If you take one of these objects individually, you will be dealing with an instance of A. The fonts, in their large majority, actually treat object and instance interchangeably. Keep in mind that instantiation of a class A will give rise to an object of the same class. (Being simplistic here, okay? Discarding more advanced concepts like inheritance, polymorphism, etc.)

    
24.03.2017 / 19:12
3
  

The instance is an object and the object is an instance? Then they would be the   same thing?

Yes and no.

On the Wikipedia page itself there are 3 paragraphs that speak the same thing in different ways. It is redundant but they did so to clarify better.

  

Instances of a class share the same set of   attributes, although they differ in their content.   attributes. For example, the "Employee" class describes the attributes   common to all instances of the "Employee" class. The objects of this   class may be similar, but vary in attributes such as "name"   and "salary". The class description contains the items corresponding to   these attributes and defines the operations or actions relevant to the   such as "wage increase" or "change in the number of   phone. "You can then talk about an instance with the name=" Joan   Rabbit "and another with the name=" João Coelho ".

     

Instance is the realization of a class. In intuitive terms, a   class is like a "template" that generates instances of a certain type; one   object is something that exists physically and that has been "shaped" in the class.

     

Thus, in object-oriented programming, the word "instantiate"   means to create. When we talk about "instantiating an object", we create   physical representation of the class. For example:   "animal" is a class or a mold; "dog" is an instance of   "animal" and although it carries all the characteristics of the mold   "animal", is completely independent of other instances of   "animal"    link

// Aqui temos uma classe. Ela ainda não é um objeto.
Class Foo{}

// Aqui criamos um objeto da classe Foo
obj = new Foo

// Repare que não usei o termo "instância" porque esse termo significa "criar". Contudo, não é errado dizer:

// Nova instância de Foo
obj = new Foo

That is, obj is objeto instantiated of class Foo .

Instance (instantiate / create) is an action. The object is a concrete representation, according to wikipedia, physically create (instantiate) a representation of the class .

The definition of what the object is is also very clear and I see no ambiguity:

  

In computer science, object is a reference to a location in the   memory that has a value. An object can be a variable, function,   or data structure. With the introduction of   objects, the word object refers to an instance of a class.

     

In object-oriented programming, an object   of a "mold" (class); the class defines the behavior of the object,   using attributes (properties) and methods (actions).    link

It is important to point out that still on the Wikipedia page there is an observation about the term instance.

  

In object-oriented programming, it is called an instance of a class,   an object whose behavior and state are defined by the class.   "Instance" is, in this case, Anglicism , meaning "case" or   "instance."

Anglicism: link

In order not to complicate matters, I will avoid comment on metaclass because flees rather context of the question. But anyway it's good to know.

The issue is addressed in the Japanese version of "instance" link

  

静 的 型 付 け の オ ブ ジ ェ ク ト 指向 言語 で は 珍 し い が, 動 的 型 付 け の オ ブ ジ ェ ク ト 指向 言語 の 多 く は, メ タ ク ラ ス を サ ポ ー ト し, ク ラ ス 自 体 も オ ブ ジ ェ ク ト と し て 扱 う こ と が で き る (ク ラ ス · オ ブ ジ ェ ク ト). ク ラ ス · オ ブ ジ ェ ク ト は,端 的 に 言 え ば 変 数 に 束縛 で き る ク ラ ス で あ る. ク ラ ス · オ ブ ジ ェ ク ト, イ ン ス タ ン ス · オ ブ ジ ェ ク ト 双方 を 変 数 に 束縛 し た 際 ど ち ら も オ ブ ジ ェ ク ト と し て 振 る 舞 い 見 か け 上 区別 は つ か な い. 例 え ば ク ラ ス · オ ブ ジ ェ ク ト, イ ン ス タ ン ス · オ ブ ジ ェ ク ト 双方 が   readFrom: と い う メ ソ ッ ド を 持 っ て い た 場合, ど ち ら も #readFrom:   メ ッ セ ー ジ を 送 っ て や る と エ ラ ー も 起 こ さ ず そ れ ぞ れ の メ ソ ッ ド を 実 行 す る.   Objective-C や Python に お い て は ク ラ ス · オ ブ ジ ェ ク ト と イ ン ス タ ン ス · オ ブ ジ ェ ク ト の 明確 な 区別 が 行 わ れ て い る. [3] [4]   メ タ ク ラ ス が サ ポ ー ト さ れ て い る シ ス テ ム で は, ク ラ ス · オ ブ ジ ェ ク ト も ま た 別 の ク ラ ス (メ タ ク ラ ス) の イ ン ス タ ン ス で あ る と い う こ と が あ り う る. こ の 場合 「ク ラ ス · オ ブ ジ ェ ク ト は イ ン ス タ ン ス で は な い」 と は 言 え な い の で, 注意 さ れ た い.

This is a brief warning in which it says not to confuse instances of metaclasses because in metaclasses instances are also classes.

As you can see, we can always complicate things further.

    
24.03.2017 / 20:54