DEV Community

Cover image for The Four Pillars of Object Oriented Programming

The Four Pillars of Object Oriented Programming

Naya Willis on November 29, 2020

In this blog I will explain the 4 pillars of OOP and provide small examples. Small enough examples that you and I can comprehend easily. ...
Collapse
 
oguimbal profile image
Olivier Guimbal • Edited

Nice article. I'd add my 2 cents: While those features are often encountered in OO languages, they are not mandatory. For instance, GO is an OO language in some way, but it does not have inheritance.

And these pillars are not an uniquely OO feature. You'll also find them in FP (Functional Programming) languages. Except maybe for inheritance, which you will of course not find in purely FP languages (Haskell, Elm, ...) - but will find it in most of them as a result of their integration in an ecosystem (Scala, F#, ...).

Richard Feldman speaks about this in one of my favorite tech talks (specifically at 19:55, but the whole talk is really worth watching)

... That said, it does not take anything away from your article :) I just wanted to share this talk.

Collapse
 
bias profile image
Tobias Nickel • Edited

Can't resist to share this post from the stackoverflow blog:

If everyone hates it, why is OOP still so widely spread?

Collapse
 
amaralani profile image
Amir Maralani • Edited

First of all thanks for a good post!
But in your encapsulation example, I see this :

let cat1 = new Dog()
...
let cow1 = new Dog()
Enter fullscreen mode Exit fullscreen mode

It works but I suggest you either initialize them from the corresponding classes or change the variable names (dog2, dog3) to make the code clean.

Collapse
 
greedybrain profile image
Naya Willis

Sorry, my mistake.

Collapse
 
dakujem profile image
Info Comment hidden by post author - thread only accessible via permalink
Andrej Rypo

Your polymorphism example breaks the Liskov substitution principle. Just use composition and don't make cow1 instanceof Dog === true πŸ€¦β€β™‚οΈ Did you study biology basics? πŸ˜‰

Collapse
 
andreidascalu profile image
Andrei Dascalu

Principle are "stuff that helps more often than not", not pervasive absolutes on the same level as God's word. But more to the point, it would be weird to talk about inheritance as a "pillar" of oop when general practices have been recommending composition over inheritance for years.

Collapse
 
greedybrain profile image
Naya Willis

My bad, changed it to inherit from the Animal class. You happy now professor?

Collapse
 
dakujem profile image
Andrej Rypo

You're welcome. Just didn't want to write that the example missed the point completely, that's all. πŸ€·β€β™‚οΈ But you're definitely on a good path, keep going!

Collapse
 
stereobooster profile image
stereobooster

Encapsulation binds together the data and functions that manipulate the data,

this is definition of object (from OOP)

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

Encapsulation

Encapsulation is a technique for minimizing interdependencies among separately-written modules by defining strict external interfaces. The external interface of a module serves as a contract between the module and its clients, and thus between the designer of the module and other designers. If clients depend only on the external interface, the module can be reimplemented without affecting any clients, so long as the new implementation supports the same (or an upward compatible) external interface. Thus, the effects of compatible changes can be confined.

-- cs.tufts.edu/comp/150CBD/readings/...

as well

encapsulate - to enclose in or as if in a capsule

-- merriam-webster.com/dictionary/enc...

Collapse
 
andreidascalu profile image
Andrei Dascalu

Nice explanation, although as far as pillars are concerned, the main one in oop is the concept of objects. Hence the name.
Although as an interesting note, some of these pillars have been noted as broken for a while now (hence the slow ride of fp)

Collapse
 
greedybrain profile image
Naya Willis

Thanks for sharing

Collapse
 
ben profile image
Ben Halpern

Nice post

Some comments have been hidden by the post's author - find out more