In Object-Oriented Programming (OOP), an OBJECT is an element that represents a real-world entity through an abstraction process.
Basically, the creators of OOP wanted to solve a problem by dividing it through small and reusable entities. With these entities, any “thing” could be modeled.
Then they thought of a name. They were looking for a word that was like “thing,” but that didn’t sound as bad as “thing.” And they said Eureka! OBJECTS (and they felt quite pleased).
In fact, let’s look at the definition of an object in the dictionary:
Object: anything that can be the subject of knowledge or sensitivity by the subject, even the subject itself 😴
Sorry, I dozed off 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 purchase order for the shoe, and the shipping address for the order
Anything you can imagine, material or abstract, large or small, green or purple, can be modeled as a 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 grouping of variables and functions, under a common name.
- stringName
- stringSurname
- DateTimeDateOfBirth
- stringGetFullName()
- stringCalculateBirthday()
OBJECTS have properties (attributes) and behaviors (methods).
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, 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.
Additionally, 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.
Creating a class: First, define a “template” or structure to create objects. This is called a class. The class defines the attributes and methods that the objects created from it will have.
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 referred to as an instance of that class.
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.
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 explore these points in more depth. Don’t worry if you don’t understand something. Remember that this is an initial introduction explaining what an Object is.
Example of an Object in Different Languages
Let’s see what the syntax would be to work with an object in different programming languages. Let’s create an object Car
that represents (guess what) a car 🚗.
This car must have:
- Attributes: Brand, model, registration year, color…
- Methods: Start (for the example, just this one)
This is how the definition of the Object, that is, its class, would look 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. 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.
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)
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)
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() }
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.