DEV Community

Cover image for Object-Oriented Programming: a Brief Introduction 👷
Pedro Furlaneto
Pedro Furlaneto

Posted on

Object-Oriented Programming: a Brief Introduction 👷

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.

:)

Top comments (3)

Collapse
 
efpage profile image
Eckehard

If you want to use OOP for web programming, the concept of HTML might give you some headaches. Definitions are always global, they cannot be encapsulated. Luckily we do not need HTML.

I created a library, that substitutes many HTML-tags with JS-functions, so, instead of

headline

you can use h1("headline") right in your code. This allow to access the DOM-references directly without the use of ID´s, setting a base for real encapsulation in JS.

The approach seems to work pretty well, even the reference page was created within days using the library. Maybe you like to check out. There are also some introductions on dev.to

Collapse
 
1canas profile image
Pedro Furlaneto

That's a nice lib bro! HTML really give me a lot of headaches

I'll try it, thank you!

Collapse
 
efpage profile image
Eckehard

You can try out most of the concepts, as the code runs directly on the reference page. Often it´s worth to have a look into the source code, as not everything is so well documented. But you will find that the approach of DML is a real gamechanger, specially if you start to use OO concepts.

You can create JS-objects that "contain" DOM-elements, no external references. That gives you the chance to use encapsulation. There is a longer post here. This works with simple functions, but is real fun with classes, as you might see with the nice clock, that was implemented as a JS class. As shown, even converting theses classes to webcomponentes is pretty easy.

So, feel free to use the lib, or even contribute to it. I just try not to use the latest JS features to preserve a better compatibility, which works pretty well.