Language: EN

que-es-una-clase-y-una-instancia-programacion

What is a class and an instance

When working with Object-Oriented Programming (OOP), there are three words that you will constantly use: OBJECTS, CLASS, and INSTANCE.

Don’t worry, you will get used to them almost effortlessly and use them constantly. But, logically, we have to explain them. Because in addition to programming well, it is also important to speak well.

Let’s start with the OBJECTS. As we already know, they are the basis of Object-Oriented Programming. We have already explained what objects are in this article.

As a quick reminder, an OBJECT is a way of modeling a part of reality. The object contains:

  • State, through fields
  • Behavior, through methods

And also, to be a “real” object, it must be treated by your program with certain considerations or particular “rules”. Some of these are:

  • Inheritance
  • Encapsulation
  • Polymorphism

Once we have briefly refreshed the concept of an Having refreshed this, we can now explain the concepts of class and instance.

Class

A CLASS is the formal definition of our Object. We can think of it as a “mold” or template for creating objects.

In the Class, we define the properties and methods that objects created from the class will have

curso-poo-clase

It will be easier to see with a simple example. Let’s say you want to create a program that works with people. To keep track of the students in a classroom, for them to buy things, or whatever you want.

The Person class contains the definition of “what a Person has”. Suppose that in your program a person is simply “something” that has:

  • name, which is a text
  • age, which is an integer.

In addition, your Person can introduce themselves (how nice). For this, it has a method that prints a message on the screen using the values of the name and age fields.

Let’s see how this would be done in different programming languages,

public class Person {
    // Fields or properties
    public string name;
    public int age;
    
    // Method
    public void Introduce() {
        Console.WriteLine($"Hi, my name is {name} and I am {age} years old.");
    }
}
class Person {
public:
	// Fields or properties
    string name;
    int age;

    // Method
    void introduce() {
        cout << "Hi, my name is " << name << " and I am " << age << " years old." << endl;
    }
};
class Person {
	// Fields or properties
    constructor() {
        this.name = "";
        this.age = 0;
    }

    // Method
    introduce() {
        console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);
    }
}
class Person:
	# Fields or properties
    def __init__(self):
        self.name = ""
        self.age = 0

	# Method
    def introduce(self):
        print(f"Hi, my name is {self.name} and I am {self.age} years old.")

Instance

Once we have defined a CLASS, we can start creating objects from the “mold” that is defined.

An instance is each of the elements created according to a class

Each INSTANCE is an independent element, which has its own values for the properties defined in the class.

In addition, of course, you can have multiple instances created from a class. In fact, it is normal to have several (or many) instances of a class.

Now let’s see it with some examples in the main programming languages. In the following code, we create two instances of the Person class:

  • One for Luis, who is 30 years old
  • Another for Maria, who is 20 years old.

In addition, we call the Introduce method of each instance to print a message in the console introducing themselves cordially, with their respective names and ages.

// Create an instance of the Person class
Person personLuis = new Person();
personLuis.name = "Luis";
personLuis.age = 30;

// Call the Introduce method
personLuis.Introduce();

// Create another instance of the Person class
Person personMaria = new Person();
personMaria.name = "Maria";
personMaria.age = 20;

// Call the Introduce method
personMaria.Introduce();
// Create an instance of the Person class
Person personLuis;
personLuis.name = "Luis";
personLuis.age = 30;

// Call the Introduce method
personLuis.introduce();

// Create another instance of the Person class
Person personMaria;
personMaria.name = "Maria";
personMaria.age = 20;

// Call the Introduce method
personMaria.introduce();
// Create an instance of the Person class
const personLuis = new Person();
personLuis.name = "Luis";
personLuis.age = 30;

// Call the Introduce method
personLuis.introduce();

// Create another instance of the Person class
const personMaria = new Person();
personMaria.name = "Maria";
personMaria.age = 20;

// Call the Introduce method
personMaria.introduce();
# Create an instance of the Person class
personLuis = Person()
personLuis.name = "Luis"
personLuis.age = 30

# Call the Introduce method
personLuis.introduce()

# Create another instance of the Person class
personMaria = Person()
personMaria.name = "Maria"
personMaria.age = 20

# Call the Introduce method
personMaria.introduce()

As we can see, each of the instances is independent of the others and has its own data. In the example, each Person has its own name and its own age.

In addition, when they call the Introduce method, they access their own values. Each instance is, wrapped and independent, with its own data.

Tips

Now some general comments and thoughts.

There is no single correct way

First of all, it is important to make it clear that there is no single way to model a person. For you, it may have name and age. But for another person, it may have name, date of birth, and ID.

Some ways will give you fewer problems than others at some point. For example, age is probably not a good idea, because it changes every year. Probably, date of birth will give you fewer problems in the future.

But still, there is no single correct way. What you include or don’t include will depend on your needs and those of your project. And a bit of “plus of cunning” of what will cause problems at some point.

The word “object”

Another special bonus is talking about the word “Object”. In principle, the concept OBJECT, CLASS, and INSTANCE are related but different concepts.

On the one hand CLASS and INSTANCE represent very different things. There is no doubt about their use here. The class is the “mold”, and the instance is “what you create” with that mold.

But regarding the word “Object”… hmm. When you are in your day-to-day conversations with other programmers, you will end up using it to refer to both class and instance.

For example, sometimes you will say “you have to create a student object”. And actually, here you are referring to creating a Student class. But other times you will say “this object here gives me a problem”, and there you are probably referring to an instance.

In the end, don’t get obsessed with it, words are just words. What is important is to understand the concepts well and that when you talk to someone, you understand each other.