DEV Community

Cover image for The Four Pillars of Object Oriented Programming
El Marshall (she/they)
El Marshall (she/they)

Posted on • Edited on

The Four Pillars of Object Oriented Programming

I've had the flu all week, so for this blog I'm covering something super fundamental. Never hurts to hear it again right? Let's take a look at the Four Pillars of Object Oriented Programming.

1. Abstraction

The abstraction of data or hiding of information, involves boiling a component down to just what elements you or the user may be concerned about. I like to think about this as creating a machine with a simple button. This machine might need to do something very simple, such as make a sound, but use a complex method to get there. It has gears and bells and timers. But the user doesn't want to operate any of that. So you give them a box, with a single button on it. They press the button. All that button does is tell the stuff in the box to do some work. The box handles the work, and Ding! A sound is produced. The user doesn't see the whole process - it is hidden from them. This is abstraction.

2. Encapsulation

When we put all those gears and bells and timers inside the box, by binding them together, we encapsulated them. All the tasks involved with making the sound have been gathered into one object - this box. If we have some other tasks, such as some bulbs mirrors for making a bright flash, we could bundle those together in a different box. Everything used for making sounds bundled in one spot, everything used for making lights bundled in another.

3. Inheritance

Inheritance is pretty much exactly what it sounds like! One class called a 'parent' class, may pass on certain properties to a 'child' class. We could make for example, a 'mammal' class, which has the child class 'cat.' 'Cat' would inherit properties from its parent 'mammal' class, such as warm-blooded, while also having some properties of its own, such as 'super cute' or 'really annoying at 3am.' Inheritance can go through multiple 'generations,' and parent or grandparent classes can have multiple child classes. One of the articles I link to in my sources below has a more full break down on different kinds of inheritance.

4. Polymorphism

Polymorphism refers to the ability of objects to literally take many (poly) forms (morph). As I understand it, this is largely thanks to inheritance. Lets take the 'mammal' class we proposed above. We could give it a function called "move()". Then each of its subclasses would have access to that function. However, when the subclass "rabbit" calls move() it may return "hop hop hop" while for a subclass "horse" you may get "clip clop clip clop." The same thing has taken multiple forms.

I hope this may serve as a helpful resource or refresher for someone! I know it was helpful for me to write this.

Sources:
https://medium.com/@hamzzza.ahmed95/four-pillars-of-object-oriented-programming-oop-e8d7822aa219
https://standardofnorms.wordpress.com/2012/09/02/4-pillars-of-object-oriented-programming/
https://stackify.com/oop-concept-for-beginners-what-is-encapsulation/
https://www.techopedia.com/definition/28106/polymorphism-general-programming

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more