que-es-un-objeto-en-programacion

What is an Object

  • 7 min

In Object-Oriented Programming (OOP), an OBJECT is an element that represents a real-world entity through a process of abstraction.

Basically, the creators of OOP wanted to solve a problem by dividing it into small, reusable entities. With these entities, any “thing” could be modeled.

So they thought of a name. They were looking for a word like “thing,” but that didn’t sound as bad as “thing.” And they said Eureka! OBJECTS (and they were quite pleased with themselves).

In fact, let’s look at the dictionary definition of object:

Object: everything that can be the subject of knowledge or sensitivity on the part of the subject, including the subject itself 😴

Sorry, I fell asleep with the definition. In short, when we say OBJECT, it basically means “anything.”

  • A vase
  • A dog and a cow
  • A person, a student, and a teacher
  • A shoe, the shoe purchase order, and the shipping address for the order

curso-poo-objetos

Everything is an object

Anything you can imagine, material or abstract, big or small, green or purple, can be modeled as an OBJECT.

Objects in programming

In programming, an OBJECT is a piece of code that models a real-world element. In general, it is a grouping of variables and functions under a common name.

Person
  • stringName
  • stringSurname
  • DateTimeBirthDate
  • stringGetFullName()
  • stringCalculateBirthday()

OBJECTS have properties (attributes) and behaviors (methods).

Properties of an object represent its internal state and are defined by variables called attributes. These attributes can be of different data types, such as integers, strings, booleans, or other objects.

Behaviors of an object are actions it can perform and are defined by methods. These methods can modify the object's state, perform calculations, interact with other objects, or perform any other specific task.

Furthermore, with composition and references, we can create relationships that allow them to interact with each other.

How to use objects

Now that we know what an OBJECT is, let’s briefly see how to work with them.

  1. Creating a class: First, you define a “template” or structure for creating objects. This is called a class. The class defines the attributes and methods that objects created from it will have.

  2. Creating an instance: Then, you can create one or more objects based on that class. This is called instantiation. Each object created from the class is called an instance of that class.

  3. Using the instance: Once you have an instance of the class, you can access its attributes and call its methods to perform various actions or manipulate its internal state as needed.

  4. Destruction (Optional): Finally, in some programming languages, it is not necessary to explicitly manage the destruction of objects, as the language automatically takes care of freeing the memory occupied by objects when they are no longer needed.

We will cover these points in more depth. Don’t be scared if you don’t understand something. Remember this is a first entry explaining what an Object is.

Example of an Object

Let’s see what the syntax would be for working with an object in different programming languages. Let’s create a Car object that represents (guess what) a car 🚗.

This car must have:

  • Attributes: Brand, model, year of registration, color…
  • Methods: Start (for the example, only this one for now)

This is how the definition of the Object, i.e., its class, would be done in different programming languages.

class Car
{
    public string Brand;
    public string Model;
    public int Year;
    public string Color;

    public void Start()
    {
        Console.WriteLine("The car is starting");
    }
}
Copied!
class Car
{
public:
    std::string Brand;
    std::string Model;
    int Year;
    std::string Color;

    void Start()
    {
        std::cout << "The car is starting" << std::endl;
    }
};
Copied!
class Car {
    constructor() {
        this.Brand = "";
        this.Model = "";
        this.Year = 0;
        this.Color = "";
    }

    Start() {
        console.log("The car is starting");
    }
}
Copied!
class Car:
    def __init__(self):
        self.Brand = ""
        self.Model = ""
        self.Year = 0
        self.Color = ""

    def Start(self):
        print("The car is starting")
Copied!

As we can see, it is very similar in almost all programming languages. Aside from the small syntax differences, the concept is the same. It is simply a grouping of data and functions under a name.

Where Does the Need Come From?

The need that OBJECTS address is ABSTRACTION. Grouping data and behavior into entities, which are a representation of something in the real world.

pilares-oop

The four pillars of OOP

What they wanted to avoid with the creation of the concept of OBJECTS is the following. Imagine you have an online store where you can process orders. For this, you have a function called ProcessOrder().

This function processes the order and returns whatever it wants the appropriate information after completing its process. For example, the information it returns could be this.

ProcessOrder() => (date, name, id, quantity, productId, price)
Copied!

The problem with this way of working is that the data is all mixed up. There are data corresponding to the buyer, the product… If each function works this way, the possibility of things getting tangled is enormous.

So they said, “we need to organize this.” Much better if we organize it into concepts, each with its own information. For example, in Person, Product, and Order.

class Person { Name, Id }

class Product { Id, Price }

class Order { Date, Quantity, Person, Product }

ProcessOrder() => (Order)
Copied!

The important thing is not only that the variables are much better organized. It is that each of them models a concept of reality.

More or less this was already being done through structures and groupings of variables. Depending on how clean you were, you would be doing it better or not. But it was already being done.

What object-oriented programming brings:

  • Standardizes and regulates the way of doing it
  • Prioritizes it over other forms of programming
  • Objects, in addition to information, have methods

That is, in the case of Order, the function ProcessOrder() could be inside.

class Order { Date, Quantity, Person, Product, ProcessOrder() }
Copied!

Was it a good idea to have functions inside our concepts? As we saw in the introduction to the course, it is something that was already being done. Object-Oriented Programming just regularized and standardized the procedure.

For this, it introduced the following pillar, ENCAPSULATION, just in the next article.