OOP Pillars

Vitor Gomes
3 min readJan 27, 2022

With the evolution of programming lineages, the procedural development was left behind by many languages, and OOP was developed in order to facilitate the mapping of things from our real-world to the data structure, besides that also in order to facilitate reading and coding, another point is allowing the reuse of the written codes, making the implementations in many of the moments easier and with high quality.

Difference Between Class and Object

Class is nothing more than something we want to map or represent with a data structure; for example, we can talk about a person.

An object is represented when your Class is instantiated, and so there starts to be represented as an Object in memory in the execution of your program.

State and Behavior

Both refer to a class below that has a Class that contains Name and BirthDay as properties and has a Calculate Age method that returns an int.

The state is represented by properties. As our properties only store the values referring to Name and DateOfBirth, this is the status of our Class. Properties are managed by using GET and SET code blocks, which will then return the current or define a new value

The behaviour is represented in the method that returns the person’s age. Behaviour is any processing we do on a class that returns data.

Heritage

In the same way that we inherit some characteristics from our relatives, we can also use inheritance in programming. With that, we can use properties and methods existing in the Class that is being inherited.

Below we have the representation that the employee inherits from the Person class. With that, you can use its states and behaviours.

OBS, be careful when creating your or refectory project, do not use an inheritance aiming at code economy.

Abstraction

The idea of abstraction is to offer a set of behaviours and state that can represent a specific part of your business domain. The abstract modifier can be used with classes, methods, properties, indexers, and events.

In an abstract class, there are methods but not implemented. That’s because the class that will inherit it can and should create their behaviour if they want to use it.

Polymorphism

I believe that Polymorphism is the simplest pillar in OOP. Nothing more is that we have two parameters options for the same method.

Encapsulation

Encapsulation is the ability to encapsulate behaviour through exposing public and private methods.

Below we have the representation of the CoffeeAutomation Class, and it has the Serve Coffee method. The Serve Coffee method creates the Espresso Machine instance, next turns on the machine, next PrepareCoffee, and next turns off the machine.

--

--