Object-Oriented Programming: a Brief Introduction πŸ‘·

Object-Oriented Programming: a Brief Introduction πŸ‘·

Β·

4 min read

Object-oriented programming is a famous programming paradigm that arises from an attempt to bring reality closer to the virtual world. It is based on the concept of objects, which are entities (concrete or abstract) that follow the same model, the classes.

πŸ€” Did You Know? πŸ€”

This concept was devised in the late 1960s by Alan Talk and was introduced through the Simula-68 language.

Classes 🏠

🏠 -> 🧱 + πŸ”¨

Classes are the "molds" of these entities, enabling the assignment of characteristics and actions to them, shaping them in the best possible way to your ideas and intentions. Think of it as the design of a house, in which it is described what is needed to make it what it is, or in other words, what is needed for a house to be a house.

One of the components that performs this descriptive function is the Attribute 🧱, which are the characteristics of an object, being similar properties that the entities of a class possess. Thus, following the previous analogy, a "House" class would have the following attributes:

Color; Roof material; Wall material; Number of bedrooms; etc...

And just like Attributes, Methods πŸ”¨ also have the function of aggregating the singularities of a Class, being responsible for representing the functionalities and behaviors of that entity through actions, or in other words, how the entity being represented acts. A house could have a method that changes its color, for example.

Special methods ⭐️

By default, classes have some special methods that optimize their use, and they are:

  • Constructors: pass standardized values ​​to the attributes in the instance of an object.

  • Getters: methods that search and return information from the attributes.

  • Setters: methods that define and update the values ​​of the attributes.

Component visibility

This definition of visibility governs external access to the methods and attributes of an object. πŸ˜Άβ€πŸŒ«οΈ

  • Public: allows access to the component anywhere the object is used.

  • Private: allows access only within the class in question.

  • Protected: allows access only to child classes, in no other application.

Relationship between Classes 🍻

The relationship between classes allows for the association between independent classes. This relationship indicates that the objects of one class are related to the objects of the other class.

There are several types of relationships, the most common in the paradigm is Association πŸ‘« which occurs when there is a structural relationship between classes, where one uses the methods and attributes of the other.

Another way for a class to relate to another is through Dependency πŸ’‰ which occurs when in one of the classes, its change affects the other, meaning that the dependency relationship is characterized when a functionality needs another class to be executed. This dependency occurs in only one of them.

Pillars of the Paradigm πŸ›οΈ

To properly understand Object Oriented Programming, it is necessary to understand the 4 pillars of the paradigm.

The main one is Abstraction πŸ“, which consists of using only the necessary characteristics in object modeling, in order to identify which attributes of the entity will actually be useful among all possible attributes.

Another pillar is Encapsulation πŸ’Š, as the term itself suggests, encapsulation "encapsulates" the object, restricting access to some of its components. Encapsulation is applied through interfaces, used for both application security and error prevention. For example, when driving a car, the driver does not have direct access to the engine, only to the pedals.

Another very important concept in the paradigm is Inheritance πŸ‘¨β€πŸ‘©β€πŸ‘§, which involves transferring the same attributes and methods from one class to others, allowing for a hierarchical tree of classes to be formed, starting from the highest generalization (root) to the highest specialization (leaves). This is considered the generalization relationship.

Lastly but not least important, Polymorphism 🐱 is the use of the same name for an existing method, but with different functionalities, either by changing their signatures or not.

Signature ✍️

The signature is what allows the identification of a method uniquely, considering that the same homonym is often used in the creation of new methods. What guarantees this uniqueness is the quantity of parameters and that these parameters are of the same type as the method of its homonym.

Basic Types of Polymorphism

  • Overriding: when inherited methods are overlaid in different classes, using the same names and signatures.

  • Overloading: when methods are added to the class, using the same name for these implementations, but with different signatures.

  • Parametric: when methods with the same homonym and signature are used, changing only the return type and parameters.

Conclusion πŸŽ‰

This was a brief conceptual presentation of the paradigm, where the main ideas were presented for its understanding. Nowadays it is possible to perceive how useful the development of this model was, which made it possible to describe in detail a real-world entity in code.

:)

Β