5 pillars of object-oriented programming
Let’s start with introductions
If we take a quick look at wikipedia to find out what object-oriented programming is, we can extract the following: Object-oriented programming (OOP) is a computer programming paradigm. It consists in the definition and interaction of software bricks called objects; an object represents a concept, an idea or any entity in the physical world, such as a car, a person or a page in a book. In simple terms, OOP is an approach to computer programming and problem solving (paradigm) that considers software elements as objects.
Let’s talk about the pillars of OOP
Like any self-respecting concept, OOP is based on pillars that reinforce its adoption. Here are the 5 essential pillars:
Object (and class)
Encapsulation
Abstraction
Polymorphism
Inheritance I admit that the words used sound rather complicated, but we’re going to demystify them one by one.
Object (and class)
Object-oriented programming means object (and very often class). A class is a mold from which objects are created. So there’s a strong relationship between class and object. A class groups together attributes (object states) and methods (object behavior). Each object created from a class will have the properties and methods defined at class level. However, the notion of class and object is not true for all languages, as Javascript is also an object-oriented programming language, but based on the notion of prototype.
Encapsulation
Behind this complicated word lies the notion of … “capsule”. This pillar is a mechanism for hiding data and certain implementations of the object, and allowing access to data only via the services offered by the object (so-called public methods). The aim is to prevent access to data in order to guarantee its integrity, and to hide certain methods that are only used within the class (implementation details).
In some programming languages, encapsulation is implemented using visibility accessors (private, protected or public).
Inheritance
Inheritance is the coolest poo concept in my opinion. This principle consists in creating a class (daughter class) that shares the characteristics of another class (called the mother class). In many programming languages, this sharing is done according to certain rules, notably linked to the level of visibility (with public, private or protected). For functions that are public or protected, the child class can inherit them. On the other hand, if a function or attribute is said to be private, the child class will not be able to access it, despite the inheritance. Sad, isn’t it?
Abstraction
We’ve still got some hard bits to swallow, but we’ll start with abstraction. Abstraction allows us to see things in more general terms, rather than looking at them as they actually are. Using abstraction, we can extract behaviors from a class that we deem “abstract”; let’s keep it simple. Abstraction allows a programmer to better design the different behaviors of a class by thinking in general rather than specific terms. In the following example, we’ll use abstraction through an abstract class.
Polymorphism
Polymorphism is a rather complicated word and a concept that’s not very well understood. Behind this big, pretty word lie two terms: “poly” meaning “several” and “morphe” meaning “form”. So, from the word polymorphism, we can derive the term several forms. There are several types of polymorphism, but the one most closely linked to the object-oriented concept is Polymorphism by sub-typing (inheritance), which consists in creating a method in the parent class that will be polymorphic. This means that the method will have several implementations, depending on the child classes that will implement them. This is the most common example of polymorphism.