JavaScript is a multi-paradigm language and can be created following various programming paradigms. A programming paradigm is basically a bunch that rules the you follow once writing code, to help you deal with a specific problem.

You are watching: 3 pillars of object oriented programming

That"s what the 4 pillars are. They"re software style principles to help you create clean Object-Orientated code.

The four pillars the object-oriented programming are:

Abstraction EncapsulationInheritancePolymorphism

Let"s take it a closer look at each of them.

Abstraction in Object-Oriented Programming

To summary something away way to hide far the implementation details within something – occasionally a prototype, sometimes a function. So when you speak to the duty you don"t have to understand precisely what the is doing.

If you had actually to recognize every single function in a huge codebase friend would never code anything. It would take month to finish reading through it all.

You can create a reusable, simple to understand, and easily changeable codebase by abstracting away specific details. Allow me give you one example:

function hitAPI(type)if (type instanceof InitialLoad) // Implementation example rather if (type instanceof NavBar) // Implementation example rather // Implementation exampleThis is not abstracted away at all.Can you check out in the example how you have to implement specifically what you need for your tradition use-case?

Every new API you need to hit demands a brand-new if block, and also it"s very own custom code. This isn"t abstracted away as you need to worry around the implementation because that every new kind you add. The isn"t reusable, and is a maintain nightmare.

How about something like the below?

hitApi("", HTTPMethod.Get)You now can just pass a URL come your duty and what HTTP an approach you desire to use and you"re done.

You don"t have to worry around how the duty works. It"s dealt with. This massively helps password reuse! and also makes your code a lot more maintainable, too.

That is what Abstraction is every about. Finding things that are similar in your code and also providing a generic function or thing to serve multiple places/with many concerns.

Here"s a good final instance of Abstraction: imagine if you were producing a device to make coffee for her users. There might be two approaches:

How to create it v Abstraction

Have a switch with the title "Make coffee"

How to create it without Abstraction

Have a switch with the location "Boil the water"Have a button with the title "Add the cold water come the kettle"Have a button with the location "Add 1 spoon of ground coffee to a clean cup"Have a button with the title "Clean any type of dirty cups"And every the various other buttons

It"s a very an easy example, however the very first approach abstracts away the logic into the machine. But the second approach forces the user come understand just how to make coffee and also essentially do their own.

The following pillar reflects us one means we can accomplish Abstraction, by using Encapsulation.

Encapsulation in Object-Oriented Programming

The definition of encapsulation is "the activity of enclosing something in or together if in a capsule". Removing accessibility to parts of her code and also making things private is exactly what Encapsulation is all about (often times, civilization refer come it as data hiding).

Encapsulation means that each object in your password should regulate its own state. State is the current "snapshot" of her object. The keys, the techniques on her object, Boolean properties and also so on. If you were to reset a Boolean or delete a crucial from the object, they"re all alters to her state.

Limit what pieces of her code deserve to access. Make more things inaccessible, if lock aren"t needed.

Private nature are accomplished in JavaScript by using closures. Here"s an instance below:

var Dog = (function () // Privatevar play = function () // beat implementation; // Privatevar breed = "Dalmatian" // Publicvar name = "Rex";// Publicvar makeNoise = role () return "Bark bark!";; return makeNoise: makeNoise,name: surname ;)();The very first thing we did was produce a duty that instantly gets called (called an Immediately Invoked role Expression, or IIFE because that short). This created an item that anyone can access but hid away few of the details. Friend can"t speak to play and you can"t accessibility breed as we didn"t expose it in the final object v the return.

This specific pattern above is called the Revealing Module Pattern, however it"s just an example of how you can attain Encapsulation.

I desire to focus more on the idea the Encapsulation (as it is more important than just learning one pattern and also counting Encapsulation as totally complete now).

Reflect, and also think much more about how you can hide away your data and code, and also separate the out. Modularising and also having clear obligations is crucial to Object Orientation.

Why must we prefer privacy? Why not just have everything global?

Lots of unrelated bits of password will come to be dependent/coupled come one an additional via the an international variable.You will likely override the variables if the surname get"s reused, which can lead to bugs or unpredictable behaviour.You will likely end up with Spaghetti Code – code that"s tough to factor through and follow what is reading and writing to your variables and changing state.

Encapsulation deserve to be used by separating out long lines of code into smaller separate functions. Separate out those features into modules. Us hide away the data in a place nothing else needs access to, and cleanly disclose what is needed.

That is Encapsulation is a nutshell. Binding her data to something, even if it is it"s a class, object, module or function, and doing your finest to save it as exclusive as you sensibly can.

Inheritance in Object-Oriented Programming

Inheritance allows one object get the properties and methods of an additional object. In JavaScript this is excellent by Prototypal Inheritance.

Reusability is the main advantage here. We understand sometimes the multiple locations need to perform the same thing, and they have to do whatever the same other than for one small part. This is a problem inheritance can solve.

Whenever we use inheritance, we shot to make it so that the parent and also the child have actually high cohesion. Cohesion is just how related your password is. Because that example, go the Bird form extend from the DieselEngine type?

Keep her inheritance an easy to understand and also predictable. Don"t inherit from somewhere fully unrelated because there"s one an approach or residential or commercial property you need. Inheritance doesn"t resolve that details problem well.

When utilizing inheritance, you need to require most of the usability (you don"t always need absolutely everything).

Developers have a principle called the Liskov Substitution principle. It states that if you deserve to use a parent class (let"s speak to it ParentType) almost everywhere you use a boy (let"s contact it ChildType) – and also ChildType inherits indigenous the ParentType – climate you pass the test.

The key reason you would fail this test, is if the ChildType is removing things from the parent. If ChildType removed techniques it inherited indigenous the parent, it"d lead to TypeError"s whereby things room undefined that you room expecting no to be.

The arrows look choose they"re walk the dorn way. But the animal is the base - the parent.

Inheritance chain is the term provided to describe the flow of inheritance indigenous the basic object"s prototype (the one that everything else inherits from) come the "end" of the inheritance chain (the last kind that is inheriting – Dog in the over example).

Do your finest to store your inheritance chain clean and also sensible. You can quickly end increase coding one anti-patterns once using Inheritance (called the Fragile base anti-pattern). This happens whereby your basic prototypes are thought about "fragile" due to the fact that you make a "safe" readjust to the base object and then start to break all your children.

Polymorphism in Object-Oriented Programming

Polymorphism means "the condition of developing in several various forms." That"s exactly what the fourth and final column is pertained to with – species in the exact same inheritance chains gift able come do different things.

If you have used inheritance correctly you can now reliably use parents favor their children. Once two species share an inheritance chain, they have the right to be supplied interchangeably with no errors or assertions in your code.

From the last diagram, we can have a basic prototype that is called animal which defines makeNoise. Then every type extending from the prototype deserve to override to carry out their own custom work. Something favor this:

// Let"s collection up an Animal and Dog examplefunction Animal()function Dog()Animal.prototype.makeNoise = function()console.log("Base noise");;// Most pets we code up have actually 4. This deserve to be overridden if neededAnimal.prototype.legs = 4;Dog.prototype = brand-new Animal();Dog.prototype.makeNoise = function()console.log("Woof woof"); ;var pet = new Animal();var dog = brand-new Dog();animal.makeNoise(); // base noisedog.makeNoise(); // Woof woof- this was overriddendog.legs; // 4! This was inheritedDog extends from Animal and can make use of the default foot property. But it"s also able to execute its very own implementation of make its very own noise.

See more: What Are Red Skelton Paintings Worth ? Red Skelton Art Paintings For Sale

The real power that polymorphism is share behaviours, and enabling custom overrides.


I hope this has explained what the 4 pillars of object-oriented programming are, and how they lead to cleaner and an ext robust code.