Objects are a way to group related variables and functions in a single container. In other words, an object is an entity that encapsulates data and functionality.
You can think of an objects as an “evolved version” of a struct, which adds additional features. Primarily, it adds that objects have functions.
For example, if we consider a class Car
, it could have properties like make
, model
, and color
, along with methods like start()
and stop()
.
- stringmake
- stringmodel
- stringcolor
- stringMake
- stringColor
- voidstart()
- voidstop()
These objects are defined by a class or a prototype that acts as a template or mold for creating objects. This defines the structure (properties and methods) common to the 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 functionalities defined by the class.
When we create a specific object of the class Car
, such as a specific car with the make “Toyota”, model “Corolla”, and color “pink”, then we have an instance of the class Car
.
Objects allow us to organize and structure the data and logic of a program, promote code reuse, and facilitate 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 moment
- Behavior: Defines the actions and operations that the object can perform
- Identity: Each object has a unique identity that differentiates it from other objects
But, much more interestingly, objects are abstractions that allow us to model the real world. They are the foundation of Object-Oriented Programming (OOP),
Example of Objects in different languages
Object-Oriented Programming has been one of the paradigms that has 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 “classical” approach, while others take somewhat different paths. But, more or less, the concept of an object exists in all languages.
Let’s look at examples in some popular languages:
// Class definition
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.");
}
}
// Creating an object
Person person = new Person();
person.Name = "Luis";
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;
}
};
// Creating an object
Person person("Luis", 25);
person.greet();
// Class definition
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.`);
}
}
// Creating an object
const person = new Person("Luis", 25);
person.greet();
# Class definition
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.")
# Creating an object
person = Person("Luis", 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 an object person
using the class and accessed its properties and methods.
Internal workings Advanced
Internally, objects are handled differently from structs due to the dynamic and object-oriented nature of the languages that use them, such as Java, Python, or C++, among others.
On 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 issues accessing the variables, as it knows exactly how much space they occupy and in what position they are.
However, an object also contains methods. Well, it’s most likely that in most languages the object is not stored with its methods (function references), but only stores the data, just like a struct.
For the methods, the compiler does “a lot of things”. Some of them include creating the object methods as functions that receive the objects as parameters. This is known as the “virtual method table” or “dispatch table”.
These details occur transparently for the programmer, as the language takes care of handling it automatically. But, if you are curious to learn more, it’s quite an interesting topic.
Object-Oriented Programming Course
The emergence of objects marked 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.
Logically, exploring them in depth is too broad for a single article. So, if you want to learn more, here is the link to the Object-Oriented Programming Course.