Understanding the concept of classes is one of the first steps to start object-oriented programming in any programming language. At first the concept may seem quite abstract and difficult to understand, but you will see that once you understand the basics and then apply it a few times to personal projects, the idea of a class in programming will become intuitive.
If you pay attention to the context in which the classes are applied within the programming, everything will start to become clearer. Being applied during object-oriented programming, the classes have the function of defining the "format" of these objects and, in addition, defining how this object will communicate with the rest of the application.
An object in turn can be anything we want to represent within our code, we can make an analogy with everyday objects like a fork, a ball, a cup, etc. These programming objects, as well as those in real life, have characteristic characteristics and behaviors. In programming we call these characteristics attributes and these behaviors methods.
To make everything clearer, let's look at an example. Think of a door lock.
A lock like the one we have on the side has some characteristics: it is a silver lock, it is made of metal and can be open or closed. In addition, it has two basic behaviors: opening and closing.
If we were to represent this lock as an object we would have what its attributes would be: color, material and current state. And its basic methods would be opening and closing.
The most important thing here is to realize the following, the characteristics of this specific lock are shared by all locks, in this way we can define a "Lock" class within which we can insert any type of lock.
So you might ask yourself, what's the point of this?
Imagine that you are a developer of a lock monitoring system, your system is responsible for monitoring the opening and closing of all the locks in a condominium, in addition, you can open or close any lock if necessary. Residents of the condominium in question are responsible for registering their own locks in their system.
By defining a lock class with the attributes and methods mentioned above, you could treat all locks in the same way regardless of any different characteristics they have between them, this facility is due to what we call encapsulation.
Conclusion
Finally, summarizing the whole explanation, we can understand the classes as molds of the objects that we use in object-oriented programming. Just like real-life objects, OOP objects have characteristics and behaviors that we call attributes and methods, respectively.
If you believe that there are more points that can be added to help understand classes in programming, leave them here in the suggestion box just below the post.
Commentaires