What is the difference between a class and an object?

Asked

Viewed 26,529 times

51

I was reading a book on object orientation and these two entities are translated differently. What is the difference between the two?

  • 3

    Class is a template/template the object is the custom template with values or status.

  • @rray thanks comrade!! You could answer the question !!

  • 1

    this question becomes interesting when we begin to question what is "object-oriented programming" and "class-oriented programming".

3 answers

48


Think of it as a matter of a construction company that needs to build houses.

Class is the plan, is the planning, is the model to be followed so that the house is built within certain characteristics. It is something abstract, it is something logical. There is defined all the elements that the house will have and the basic characteristics of how they will be composed. It exists only in the code. Class typifies what will be modeled by it. It determines the possible states and behaviors that objects can have.

Planta

The object is the house. It is something concrete, something physical. In it the elements are actually present there. It is something palpable (in computer terms), it is something that can be manipulated. It exists in memory, during the execution of the application. Object has values for the defined states and calls the defined behaviors running the algorithms. It has a transient life time.

Prédio

So the object is an instance of the class. In the class you can say that that object will have a color, in the object you say what the color is, you can only say this because it was set in the class that this information should be in the object.

Contrary to what many believe an object does not need to be created based on a class. For example a int does not need a class to define it, but it is still an object when instantiated (1 is an instance).

  • 13

    Well thought out, just to illustrate a little more. Now think of a allotment of several equal houses, those that the government gives to the people. Has a plant (Classe) on which all houses will be based, that is, same number of rooms, same bathroom number, etc. The Objects would be the ready house, each house has a different inhabitant, different furniture, ie possess atributos different. Summarizing all the houses are equal in their structures, but have different elementros inside. Objects of an X class are all equal, what differentiate are the attribute values they have.

  • 10

    Okay + to array :D Casa[] cohab = new Casa[1000];

  • hehehe, is that I meant that from a class, it is made several instances.

  • In the edition you placed 3 image links, but only used 2. Forgot the image 2 or changed his mind and did not remove the link?

  • I think I didn’t remove the link, it was only supposed to be these two. I thought the system would do this.

18

Interesting that despite being a trivial question, when I was learning about object orientation I talked with several experienced programmers and nobody could explain to me, in a clear way, what is a class and what is an object.

I just went to learn and understand what the difference was when I started programming and could see, in practice, what was just theory for me.

The class is a model, a planning, like the model of a house. This house has several features that are not expressed in the model (class) such as color, whether it is left over or not, whether it has garage or not, when it was built, what the venal value, what the built area, etc. This is class.

The object would be the materialized class, that is, an object with the proper attributes qualified: a blue house, ground floor, with garage, built in 2015, with venal value of $ 100,000.00, with built area of 60m2, etc. This is object, also known as instance of class. The instance occupies space in memory as a house occupies space on a land.

The same class can give rise to several objects, all are houses, but each will have different characteristics.

The same analogy can be applied to the concept of composition. A class can have an attribute that is another class. For example, the Home class may have an attribute called Insurance. This Insurance attribute is another class that has its own attributes such as Insurance, Indemnity, Early Term, etc.

Another analogy can be made with inheritance. A house can extend a class called Edification. A House is a Building just as an Igloo is. The Building class has its own attributes, such as Construction Date, etc. All these attributes make sense to any class that extends Edification.

And the concept of implementation (used in the Java language, for example). A home can implement the Chimney interface. In this case, all the houses that implemented Chaminé would be obliged, for example, to say in which location of the house will be installed the chimney. This would be an implemented interface method.

  • I have this doubt. However, these answers helped a lot.

  • Actually the model would be a prototype that is also used in object orientation, but it is not a class that works even like a plant.

3

Another way to explain using a person as an example

Class

Rules (logic), adjectives(Attributes) and verbs(methods). Written part, the file in your project.

Object

The person himself (Instance), John. Data that is in memory and loaded at execution.

Reading books about Orientation objects sometimes the author writes about an Abstraction naming (referencing) as an Object, it confuses a little, in practice (code running) the Object is the same as instance, in the books 'sometimes' I believe the author was referring to a class, the part actually written and not to the object in memory (instance).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.