What is the difference between a class and an object?

33

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

    
asked by Pena Pintada 28.11.2015 в 00:49

2 answers

30

Think of it as a question of a builder who needs to build houses.

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

The object is the house. It is something concrete, something physical. In it the elements are indeed present there. It is something palpable, it is something that can be manipulated. It exists in memory. Object has values for the defined states and call the behaviors. It has a transient lifetime.

    
28.11.2015 / 00:54
11

Interesting that despite being a trivial question, when I was learning about object orientation I talked to a number of experienced programmers and no one could clearly explain what a class is and what an object is.

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

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

The object would be the materialized class, that is, an object with the proper qualified attributes: a blue, one-story house with garage, built in 2015, with a market value of $ 100,000.00, with built area of 60m2, etc. This is an object.

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

    
02.12.2015 в 13:31