Object-Oriented Programming Paradigm

OOP is a paradigm focused on modeling systems as collections of objects, where each object represents a specific aspect of the system's data and behavior.

Everything’s an object. This assertion underlies Object-oriented programming (OOP). In this paradigm, objects are well-defined data structures, each with its own properties (attributes) and the capability to run its own procedures (methods).

Some of the primary benefits of this approach include well-structured codebases and programs that are easier to modify, debug, maintain, and reuse.

Prior to ES6, JavaScript aimed to emulate traditional class-based languages like Java, C++, and Python. But JS remains the most popular among them, despite of being much less of a class-based language.

In the JS way, the class syntax is simply syntactic sugar over function constructors and Prototypal Inheritance. When we share data or functionality between objects for example, a child object accessing a parent object’s methods/ props, we use the built-in prototype system.

How the Prototype Chain Works:

  • When you access a property, the JS interpreter first looks at the instance object itself.
  • If it isn't found, it travels up the prototype chain using the __proto__ pointer.
  • It then searches the linked prototype objects until it finds the property or reaches its 'Object.prototype' (the root).
  • When a new object is created in JS, we're instantiating a reference to a specific location in memory and its corresponding prototype.

Principles of OOP:

  • Encapsulation,
  • Abstraction,
  • Inheritance and
  • Polymorphism.

1. Encapsulation

  • The principle of encapsulation entails that all properties and methods of an object are kept self-contained and safe from outside interference.
  • Within an object, we can define both private and public members. Private variables and methods are inaccessible to other objects,
  • whereas public ones provide a controlled interface for interaction. This prevents the internal state of an object from being modified unexpectedly by external code.
let employee = {
    baseSalary: 50000,
    overtime: 10,
    rate: 20,
    // This method uses 'this' to access its own encapsulated data
    getWage: function() {
        return this.baseSalary + (this.overtime * this.rate);
    }
};

 console.log(employee.getWage()); // 50200

2. Abstraction

Abstraction helps isolate the impact of changes made to the code so that if something goes wrong, the change will only affect the implementation details of a class and not the outside code.

Think of a music player: you see a "Play" button, but the complex circuitry required to decode the audio is hidden inside. We understand and know the technology on the surface but might not know how it works inside or might not have an urge to understand how it works. By hiding internal complexity and only exposing the essential features, we prevent "leaking" implementation details that the rest of the program doesn't need to know about.

3. Inheritance

Under the Inheritance mechanism, a class inherits the features, props, and/ or methods of another class. In JS, this is achieved through the prototype chain, at the same time promotes reusability, and ensures that shared functionality isn't duplicated in memory.

4. Polymorphism

Polymorphism (meaning "many shapes") allows different objects to be treated as instances of the same general type through a shared interface. It helps eliminate long if/else or switch statements by allowing each unique object to implement its own specific version of a common method. When that method is called, the object performs the action according to its own logic.

Example:

// Each element (Circle, Square, Triangle) has its own render logic,
// but we can call .render() on all of them in one loop.

elements.forEach(element => element.render());

Key Benefits of OOP

  • Encapsulation: Reduces complexity and increases reusability.
  • Abstraction: Isolates the impact of changes by hiding implementation details.
  • Inheritance: Eliminates redundant code by sharing logic across objects.
  • Polymorphism: Refactors messy conditional logic into clean, object-specific behavior.

CAKE®STACK

Rolling With The Dough