Welcome ðŸŽ‰

logo

ReactLMS

Search
Light Mode
Contact Us

4 min to read

Contact us

No results for your search.
Sorry, an unexpected error occurred

Concept


In object-oriented programming, the concepts of encapsulation, inheritance, polymorphism, and abstraction are fundamental and important.


Object-oriented programming operates with 4 principles:


Encapsulation


Encapsulation refers to hiding information inside an object and only allowing access through public methods. This helps protect data and ensure the integrity of the object. In JavaScript, we can use class to implement encapsulation. Here is an example:

class BankAccount {
  constructor(accountNumber, balance) {
    this.accountNumber = accountNumber;
    this.balance = balance;
  }

  deposit(amount) {
    this.balance += amount;
  }

  withdraw(amount) {
    if (amount <= this.balance) {
      this.balance -= amount;
    } else {
      console.log("Insufficient funds.");
    }
  }

  getBalance() {
    return this.balance;
  }
}

const myAccount = new BankAccount("123456789", 1000);
console.log(myAccount.getBalance()); // Output: 1000
myAccount.deposit(500);
console.log(myAccount.getBalance()); // Output: 1500
myAccount.withdraw(200);
console.log(myAccount.getBalance()); // Output: 1300






In the above example, we have a class BankAccount with attributes accountNumber and balance. We can only access and modify the values of these attributes through public methods like deposit, withdraw, and getBalance.


Inheritance


Inheritance allows us to build child classes based on a parent class, inheriting the attributes and methods of the parent class. In JavaScript, we can also use class to implement inheritance. Here is an example:

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

  speak() {
    console.log(`${this.name} is speaking.`);
  }
}

class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }

  bark() {
    console.log(`${this.name} is barking.`);
  }
}

const dog = new Dog("Buddy", "Golden Retriever");
dog.speak(); // Output: Buddy is speaking.
dog.bark(); // Output: Buddy is barking.






In the above example, we have a parent class Animal with attribute name and method speak. The child class Dog inherits from the parent class Animal and adds its own method bark.


Polymorphism


Polymorphism allows objects of the same type to understand and perform methods differently. In JavaScript, we can use class to implement polymorphism. Here is an example:

class Shape {
  calculateArea() {
    // Phương thức này sẽ được ghi đè trong các lớp con
  }
}

class Rectangle extends Shape {
  constructor(width, height) {
    super();
    this.width = width;
    this.height = height;
  }

  calculateArea() {
    return this.width * this.height;
  }
}

class Circle extends Shape {
  constructor(radius) {
    super();
    this.radius = radius;
  }

  calculateArea() {
    return Math.PI * this.radius * this.radius;
  }
}

const rectangle = new Rectangle(5, 10);
const circle = new Circle(3);

console.log(rectangle.calculateArea()); // Output: 50
console.log(circle.calculateArea()); // Output: 28.274333882308138






In the example above, we have a parent class Shape with the method calculateArea. The child classes Rectangle and Circle inherit from the parent class and override the calculateArea method to calculate the area for a rectangle and a circle.


Abstraction


Abstraction allows us to define abstract classes that cannot create objects from them. In JavaScript, we do not have the abstract keyword to define abstract classes, but we can use class to create abstraction. Here is an example:

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

  speak() {
    console.log(`${this.name} is speaking.`);
  }
}

class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }

  speak() {
    console.log(`${this.name} is barking.`);
  }
}

const animal = new Animal("Generic Animal");
const dog = new Dog("Buddy", "Golden Retriever");

animal.speak(); // Output: Generic Animal is speaking.
dog.speak(); // Output: Buddy is barking.






In the example above, the class Animal is an abstract class, which cannot create objects directly from this class. The child class Dog inherits from the Animal class and overrides the speak method to produce different results.


Read more
On This Page