profile image

CODES REALM

A Software & Cloud Engineering Journal.

Quick Tags

C# / .Net

JavaScript / TypeScript

Azure

Kotlin / Java

Notes

Azure Certification Notes

Back to home

Pillars of Objected-Oriented Programming

This post is part of a series of posts on JavaScript object-oriented programming. Link to the series are as follows:

This series is as follows:

  1. Pillars of Objected-Oriented Programming
  2. JavaScript Objects, prototype, constructor, & Object constructor
  3. Object Creation in JavaScript
  4. JavaScript OOP : Abstraction
  5. JavaScript OOP : Encapsulation
  6. JavaScript OOP : Inheritance
  7. JavaScript OOP : Polymorphism
  8. JavaScript OOP : Inheritance vs Composition

Object-oriented programming has four pillars which we will quickly go over. These pillars or concept define or set some guidelines on how to write codes to represent objects with characteristics and behaviours. E.g., A person may be described as an object with attributes like first name, last name, date of Birth and a person can eat, walk, run, and dance.

The four pillars are namely, Abstraction, Encapsulation, Inheritance, and Polymorphism.

Abstraction

An abstraction in object-oriented programming (OOP) is the process of hiding away the complexity of an object's internal implementation details and present relevant information without noise. An example of abstraction in the real world is a simple cassette player

Casset player

This cassette player has few buttons on it, but the implementation details of how the electronics embedded inside work are abstracted away, it's hidden, and all that's useful to a music listener is the play, pause, forward, rewind and record buttons. The details of how the capacitors, diode , resistors, circuit board work is of no importance and as a music listener, I don't know want know either.

Encapsulation

In a simple sentence, encapsulation is the process of hiding internal details or implementation from the outside such that it helps prevent tampering and accidental modification. As you programming more and create objects, you will quickly realise some details should never leak as a result of our code which could lead to serious problems like data breach, losing sensitive data and confidentiality. Different programming languages use different implementation to achieve encapsulation. In the coming post, we will see how we can implement encapsulation in JavaScript which is a bit different from strongly typed languages like C# and Java.

Inheritance

Inheritance in OOP is just like inheritance in real life whereby a child inherits the parent(s) trait(s) such as eye colours, height etc. Inheritance describes an "IS-A" relationship e.g. a dog is an Animal etc. Inheritance in OOP helps reduce code duplication by re-using codes or sharing codes. E.g. You are writing an application for a vet. You have to ensure that your application can handle check-in and check-out of all kinds of animal. The only way to make this possible is by re-using code. In this case, you will have a base object called Animal with general characteristics animal characteristics and have, e.g. dogs; cat inherits from the Animal class. In conclusion, inheritance is the ability of a child object to inherit a parent object properties and behaviours in addition to its own.

Polymorphism

Poly means many, and morphism means forms. Polymorphism is the ability of an object to exist or represented in many forms. In OOP, child or children object may inherit behaviour from a parent object and override or implement that object differently from the parent. In conclusion, polymorphism is the ability of an object to change behaviour at runtime.

Composition

Composition is an important OOP concept worth mentioning. It's not one of the four listed pillars of OOP, but OOP is not complete without it. Unlike inheritance, composition is a "HAS-A" relationship. A dog is an animal, but a dog has an owner. An object representing a dog will relate to the animal object by inheritance, but relate to owner through composition.

This concludes the information needed to delve into the concepts of OOP deeper.

Object-Oriented JavaScript