OOP🚌

_Khojiakbar_ - Jul 7 - - Dev Community

1. What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data (in the form of properties) and code (in the form of methods). JavaScript supports OOP through prototypes and ES6 classes.

2. Basic Concepts

Class: A blueprint for creating objects (instances).

Object: An instance of a class.

Constructor: A special method for initializing new objects.

Method: A function defined inside a class.

Inheritance: A way for one class to extend another class.

Encapsulation: Keeping the details (state) of an object hidden from the outside world.

Abstraction: Simplifying complex systems by modeling classes appropriate to the problem and working at the most relevant level of inheritance.

Polymorphism: Using a single interface to represent different underlying forms (data types).

3. Creating a Class

In ES6, you can create a class using the class keyword.

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

// Funny Example: A talking cat
class Cat extends Animal {
  speak() {
    console.log(`${this.name} says: Meow!`);
  }
}

const fluffy = new Cat('Fluffy');
fluffy.speak(); // Fluffy says: Meow!
Enter fullscreen mode Exit fullscreen mode

4. Encapsulation

Encapsulation is the practice of keeping the internal state of an object hidden and only allowing access through methods.

class SecretAgent {
  #realName; // Private field

  constructor(alias, realName) {
    this.alias = alias;
    this.#realName = realName;
  }

  revealIdentity() {
    return `My real name is ${this.#realName}.`;
  }
}

const bond = new SecretAgent('007', 'James Bond');
console.log(bond.alias); // 007
console.log(bond.revealIdentity()); // My real name is James Bond.
console.log(bond.#realName); // SyntaxError: Private field '#realName' must be declared in an enclosing class
Enter fullscreen mode Exit fullscreen mode

5. Inheritance

Inheritance allows one class to inherit properties and methods from another class.

class Dog extends Animal {
  speak() {
    console.log(`${this.name} says: Woof!`);
  }
}

const rex = new Dog('Rex');
rex.speak(); // Rex says: Woof!
Enter fullscreen mode Exit fullscreen mode

6. Abstraction

Abstraction in programming means hiding the complex implementation details and showing only the necessary features of an object. It's like dealing with a simple interface without worrying about the complicated internal workings.

1. Simple Example: Coffee Machine

Think of a coffee machine. You press a button, and coffee comes out. You don't need to know how the machine heats the water, grinds the coffee beans, or mixes everything together. All those details are abstracted away.

Here's how this would look in JavaScript:

class CoffeeMachine {
  makeCoffee() {
    this.boilWater();
    this.brewCoffee();
    this.pourInCup();
    console.log("Here's your coffee!");
  }

  boilWater() {
    console.log("Boiling water...");
  }

  brewCoffee() {
    console.log("Brewing coffee...");
  }

  pourInCup() {
    console.log("Pouring coffee into cup...");
  }
}

const myCoffeeMachine = new CoffeeMachine();
myCoffeeMachine.makeCoffee();
// Boiling water...
// Brewing coffee...
// Pouring coffee into cup...
// Here's your coffee!
Enter fullscreen mode Exit fullscreen mode

In this example, the makeCoffee method provides a simple interface to make coffee, while the internal methods boilWater, brewCoffee, and pourInCup are hidden from the user.

2. Another Funny Example: Magic Show

Imagine a magician performing a trick. The audience only sees the magic trick but not the preparation and setup behind it.

class Magician {
  performTrick() {
    this.prepareProps();
    this.doMagic();
    this.hideProps();
    console.log("Abracadabra! The trick is done!");
  }

  prepareProps() {
    console.log("Preparing magic props...");
  }

  doMagic() {
    console.log("Performing the magic trick...");
  }

  hideProps() {
    console.log("Hiding magic props...");
  }
}

const magician = new Magician();
magician.performTrick();
// Preparing magic props...
// Performing the magic trick...
// Hiding magic props...
// Abracadabra! The trick is done!
Enter fullscreen mode Exit fullscreen mode

Here, the performTrick method provides a simple interface for performing a magic trick, while the internal methods prepareProps, doMagic, and hideProps are hidden from the audience.

7. Polymorphism

Polymorphism lets you use a single interface to interact with objects of different types.

class Animal {
    constructor(name) {
      this.name = name;
    }

    speak() {
      console.log(`${this.name} makes a noise.`);
    }
  }

class Bird extends Animal {
  speak() {
    console.log(`${this.name} says: Tweet!`);
  }
}

  class Cat extends Animal {
    speak() {
      console.log(`${this.name} says: Meow!`);
    }
  }
Enter fullscreen mode Exit fullscreen mode

8. Real-Life Funny Example

Let's create a simple game where different characters (like superheroes) perform actions.

class Superhero {
  constructor(name, superpower) {
    this.name = name;
    this.superpower = superpower;
  }

  usePower() {
    console.log(`${this.name} uses ${this.superpower}!`);
  }
}

class SpiderMan extends Superhero {
  usePower() {
    console.log(`${this.name} shoots webs!`);
  }
}

class IronMan extends Superhero {
  usePower() {
    console.log(`${this.name} fires repulsor beams!`);
  }
}

const heroes = [new SpiderMan('Spider-Man'), new IronMan('Iron Man')];

heroes.forEach(hero => hero.usePower());
// Spider-Man shoots webs!
// Iron Man fires repulsor beams!
Enter fullscreen mode Exit fullscreen mode

Conclusion

OOP in JavaScript helps you structure your code more efficiently by using classes, objects, inheritance, encapsulation, abstraction, and polymorphism. These concepts make it easier to manage and scale your code, especially in large projects. And with some creativity, you can make learning these concepts a lot more fun!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player