Object-Oriented Programming Concepts in JavaScript

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

Encapsulation

Before Object-Oriented Programming, we have procedural programming where we just have functions modifying data with no real structure. Object-Oriented Programming puts things in an Object in a container and organizes things into units that model real-world applications.

The encapsulation that we wrap the code into boxes that are related to one another. So that these boxes can just interact with each other using the methods and properties that we can use. This makes code easier to maintain and more reusable. Again, encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object’s components. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them.

Abstraction

An abstraction is a way of hiding the implementation details and showing only the functionality to the users. In other words, it ignores the irrelevant details and shows only the required one. This reduces complexity because we can just see the methods and what the class can do. Now the idea of abstraction also helps when we have private variables and private methods in JavaScript.


function Person(name, country) {
    this.name = name;
    this.country = country;
    let language = "english";
    this.speak = function () {
        return think()+ " and " + this.name + " can speak " + language;

    }
    let think = function () {
        return "Think by " + language
    }
}

const tim = new Person("tim", "USA");
console.log(tim.speak());
Think by english and tim can speak english

let think is a part of Abstraction method. Also, a language field and a think method is only used in Person function. When you create a tim object you can not use the field and method directly like private Java.

Inheritance

By inheriting from other classes, we avoid having to rewrite the same code and we also save memory space by having shared methods. Inheritance is a very important and powerful concept in JavaScript.

Ploymorphism

The definition of polymorphism is actually heavily debated. But the idea is the ability to call the same method on the different objects and each object responding in different ways. The idea with a polymorphism in Object-Oriented Programming is that it has the ability to process objects differently depending on their data type or class because JavaScript is a dynamically typed language. It actually limits the amount of polymorphism that we can have but the idea is still the same. The ability to redefine methods for child classes in allowing us to reuse some of the functionalities but also customize methods to their own objects and classes and polymorphism is useful.

Inheritance vs polymorphism

  • Inheritance allows code reusability and Inheritance allows the already existing code to be reused again in a program.
  • polymorphism provides a mechanism to dynamically decide what form of a function to be invoked.

Polymorphism

class Person {
    constructor(name, country, language) {
        this.name = name;
        this.country = country;
        this.language = language;
    }
    speak() {
        return this.name + " can speak " + this.language;
    }
}

class American extends Person {
    constructor(name, country, language, location) {
        super(name, country, language);
        this.location = location;
    }
    speak() {
        console.log(super.speak())
        return "An American " + this.name + " can speak " + this.language;
    }

}

const tim = new American("tim", "USA", "english", "North");
console.log(tim.speak());

the same named speak() method or function is used in different class.

Leave a Reply

Your email address will not be published.

ANOTE.DEV