The Four Pillars of Object Oriented Programming

Karl Matthes
The Startup
Published in
4 min readSep 28, 2020

--

Object-oriented programming’s goal is on creating objects, entities with fields and methods. In terms of parts of speech, you may think of objects as nouns, fields as adjectives, and methods as verbs. With the noun being the focus, adjectives will describe the noun, and the verbs are actions that the noun is capable of. In the same vein of this comparison, objects are good at emulating real-life things: with a Dog object, it may have fields for name and age, and methods for eating and barking. Again, the name and age describe properties, attributes, or state of the dog, and the methods are actions the dog can take.

This is just the basic structure of an object in object-oriented programming. For an object to take full advantage of this programming paradigm, it should follow the four principles of object-oriented programming: encapsulation, abstraction, inheritance, and polymorphism.

Encapsulation

Encapsulation refers to an object keeping parts of itself private and all in one place. For a Dog object, everything about the dog would kept inside the object, and would be kept private. The dog may have fields for their name, age, mood, and favorite toy. Those fields would be kept private and could not be changed directly. Instead, to change those fields, the Dog would need to have a public function that changed those private fields. The difference is between directly trying to reassign a variable, like:

dogName = "Rover"

versus invoking a public function to do it:

setDogName("Rover")

If this object is following the principle of encapsulation, that first option should not work because the field would be private, and the second option should be the only way to change it.

Abstraction

Abstraction starts where encapsulation leaves off, and is about only exposing those private fields through public functions only as needed. In the last, example, setDogName would be a pretty good function to have, as a dog’s name may change; maybe it becomes a stray and gets new owners. The dog’s age, however, shouldn’t be allowed to changed excepted by the dog naturally aging. The owner can’t determine or change the age of the dog. But for something like mood, the owner shouldn’t be able to change it directly, but they should have ways to influence it. Perhaps there is a public playFetch() function which the owner can invoke, and playFetch() will improve the dogs mood, but also leave the dog more tired and hungry. And if the dog is very tired after playing, the dog will invoke its own private sleep() function. This is the focus of abstraction: preventing private fields and functions from being accessed directly, while creating an interface for them to be changed and keep some of the private fields and functions out of sight.

Inheritance

With Dog as a sort of blueprint, you can create other types of dogs based on it. If a Dog has function like eat(), sleep(), and play(), every object that inherits from Dog will also have access to those function. As an example, Catahoula Leopard Dogs are dogs, and they are quite good at climbing trees. In this case, we might say that a CatahoulaLeopardDog object would inherit from Dog, and have all the Dog functions of eat(), sleep() and play(), but it would also have its own personal function, climbTree(). A Bloodhound object would inherit from Dog and then have a trackScent() function, or Dachshund may have burrow(). Every object that inherits from Dog will have eat(), sleep(), and play(), and Dog will not have access to these unique functions that the Bloodhound, Dachshund, and CatahoulaLeopardDog objects have.

Polymorphism

Alternatively, one of these special type of dogs could be inheriting from Dog, but changing one of the functions that it inherited. Let’s say Dog also has a bark() function, and a Chihuahua object is inheriting from Dog. The Chihuahua object will have access to a bark() function because it inherited it from Dog, but when it invokes that bark(), it comes out as a “yip”. Or in the case of a Husky object inheriting from Dog, maybe its bark() function produces a howl instead. This is the principle of polymorphism, being able to inherit fields and functions, and then changing them as needed.

Together, these four principles are what make up objects and object-oriented programming. If implemented correctly, the end result should be code that is more organized, efficient, and modular.

--

--