Language: EN

que-es-un-objeto

What is and how to use Objects

Objects are a way to group related variables and functions into a single container. That is, an object is an entity that encapsulates data and functionality.

You can think of an object as an “evolved version” of a structure, which adds additional features. Mainly, it adds that objects have functions.

For example, if we consider a “Car” class, it could have properties like “brand”, “model” and “color”, along with methods like “start” and “stop”.

curso-programacion-object

These objects are defined by a class or a prototype that acts as a template or mold for creating objects. These define the common structure (properties and methods) shared by objects of that type.

Each of the objects created according to a class is called an instance. Thus, an instance is a concrete representation of a class, with its own unique data and the functionalities defined by the class.

When we create a specific object of the “Car” class, such as a specific car with the brand “Toyota”, model “Corolla” and color “pink”, we are creating an instance of the “Car” class.

curso-programacion-objeto-coche

Your car instance, different from all the others

Objects allow us to organize and structure the data and logic of a program, promote code reusability, and facilitate its maintenance.

Each of these autonomous units, which we call objects, has the following characteristics:

  • State: Represents the data and properties of the object at a given time
  • Behavior: Defines the actions and operations that the object can perform
  • Identity: Each object has a unique identity that sets it apart from other objects

But, even more interestingly, objects are abstractions that allow us to model the real world. They are the basis of object-oriented programming (OOP),

The appearance of objects was one of the most important and widespread paradigms in programming, object-oriented programming (OOP). The paradigm that has had the most impact on how we develop software.

Object-oriented programming is a field too broad to cover in a single entry. So we will see it soon in its own course.

Example of Objects in different languages

Object-oriented programming has been one of the paradigms that have had the most impact on modern programming. Consequently, many languages have evolved to introduce objects into their syntax.

The details vary from one language to another. Some have a more “classic” approach, and others take somewhat different paths. But, more or less, the concept of an object is in all languages.

Let’s see examples in some popular languages:

// Definition of a class
class Person
{
    // Properties
    public string Name { get; set; }
    public int Age { get; set; }

    // Method
    public void Greet()
    {
        Console.WriteLine("Hello! My name is " + Name + " and I am " + Age + " years old.");
    }
}

// Creation of an object
Person person = new Person();
person.Name = "Juan";
person.Age = 25;
person.Greet();
#include <iostream>
#include <string>
using namespace std;

class Person
{
    // Properties
    string name;
    int age;

public:
    // Constructor
    Person(string name, int age)
    {
        this->name = name;
        this->age = age;
    }

    // Method
    void greet()
    {
        cout << "Hello! My name is " << name << " and I am " << age << " years old." << endl;
    }
};

// Creation of an object
Person person("Juan", 25);
person.greet();
// Definition of a class
class Person {
  // Constructor
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  // Method
  greet() {
    console.log(`Hello! My name is ${this.name} and I am ${this.age} years old.`);
  }
}

// Creation of an object
const person = new Person("Juan", 25);
person.greet();
# Definition of a class
class Person:
    # Constructor
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Method
    def greet(self):
        print(f"Hello! My name is {self.name} and I am {self.age} years old.")

# Creation of an object
person = Person("Juan", 25)
person.greet()

In the previous examples, we created a class called Person that defines properties (like name and age) and a method (like greet) common to all objects of that class.

Then, we created a “person” object using the class and accessed its properties and methods.

Internal operation Advanced

Internally, objects are handled differently than “structs” due to the dynamic and object-oriented nature of the languages that use them, such as Java, Python, C++, among others.

On the one hand, similar to structs, when an object is created in most object-oriented languages, a contiguous block of memory is reserved that contains all its attributes.

This ensures that related data is stored together, allowing efficient access to it. The language has no problem accessing the variables, as it knows exactly how much space they occupy, and where they are located.

But, an object also contains methods. Well, most likely in most languages the object is not stored with its methods (references to functions), it only stores the data, just like a struct.

For methods, the compiler does “many things”. Some of them are creating the methods of the objects as functions that receive the objects as parameters. This is known as a “virtual method table” or “dispatch table”.

These details happen transparently to the programmer, as the language automatically handles it. But, if you feel like looking for more, it’s a pretty interesting topic.