Language: EN

que-es-un-objeto-en-programacion

What is an Object

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 and reusable entities. With these entities, anything could be modeled.

So, they came up with a name. They were looking for a word that was like “thing,” but didn’t sound as bad as “thing.” And they said Eureka! objects… and they were so pleased with themselves.

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

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

Sorry, I fell asleep with the definition. In summary, 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, large 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 an element of the real world. In general, it is a group of variables and functions under a common name.

Objects have properties (attributes) and behaviors (methods). And they look like this,

Person {
	// properties / data
	string Name;
	string LastName;
	DateTime BirthDate;

	// methods / behavior
	string GetFullName();
	string CalculateBirthday();
}
  • The 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, text strings, booleans, or other objects.

  • The 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.

In addition, 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.

  • Creation of a class: First, you define a “template” or structure to create objects. This is called a class. The class defines the attributes and methods that objects created from it will have.
  • Creation of 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.
  • Utilization of 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.
  • Destruction (Optional): Finally, in some programming languages, explicitly managing the destruction of objects is not necessary, as the language automatically handles releasing the memory occupied by the objects when they are no longer needed.

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

Example of an Object in different programming languages

Let’s see how the syntax for working with an object in different programming languages would look. Let’s create an object Car 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)

This is how the definition of the Object, that is, its class, would be 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");
    }
}
class Car
{
public:
    std::string Brand;
    std::string Model;
    int Year;
    std::string Color;

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

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

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

As we can see, it is very similar in almost all programming languages. Apart from the usual small differences in syntax, 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 cover is ABSTRACTION. Grouping data and behavior into entities, which are a representation of something from 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 that 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 performing its process. For example, the information it returns could be this.

ProcessOrder() => (date, name, ID, quantity, ID, price)

The problem with this way of working is that the data is all mixed up. There is data related to the buyer, to the product… If each function works like this, the possibility of things going wrong is enormous.

So they said, “we need to organize this.” It’s 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)

The important thing is not only that the variables are much better organized. It is also that each one 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:

  • Standardizing and regulating the way of doing it
  • Prioritizing 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 included within.

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

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 simply regulated and standardized the process.

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