Command Palette

Search for a command to run...

Abstract vs Abstraction in OOP (with TypeScript)đŸĨˇ
shantow-67
shantow-67
¡4 min read

Abstract vs Abstraction in OOP (with TypeScript)đŸĨˇ

  • â–ļī¸Abstract āϕ⧀

  • â–ļī¸Abstraction āϕ⧀

  • â–ļī¸Abstract āĻ•āĻŋāĻ­āĻžāĻŦ⧇ TypeScript-āĻ implement āĻ•āϰāĻž āĻšā§Ÿ

  • â–ļī¸OOP-āϤ⧇ āĻ•āĻŋāĻ­āĻžāĻŦ⧇ Abstraction principle achieve āĻ•āϰāĻž āϝāĻžā§Ÿ

OOP (Object-Oriented Programming) āĻļ⧇āĻ–āĻžāϰ āĻĒāĻĨ⧇ āφāĻŽāϰāĻž āĻĒā§āϰāĻžā§Ÿāχ Abstract āφāϰ Abstraction āĻāχ āĻĻ⧁āχāϟāĻž āĻļāĻŦā§āĻĻ āĻļ⧁āύāĻŋāĨ¤ āĻ…āύ⧇āϕ⧇ āϧāϰ⧇ āĻ¨ā§‡ā§Ÿ āĻāϟāĻž āĻāĻ•āχ āϜāĻŋāύāĻŋāϏ, āĻ•āĻŋāĻ¨ā§āϤ⧁ āύāĻž — āĻāϰāĻž āφāϞāĻžāĻĻāĻž, āϤāĻŦ⧇ āĻāϕ⧇ āĻ…āĻĒāϰ⧇āϰ āϏāĻžāĻĨ⧇ āϘāύāĻŋāĻˇā§āĻ āĻ­āĻžāĻŦ⧇ āϏāĻŽā§āĻĒāĻ°ā§āĻ•āĻŋāϤāĨ¤

🧠 What is Abstraction? (āĻ…ā§āϝāĻžāĻŦāĻ¸ā§āĻŸā§āϰāĻžāĻ•āĻļāύ āϕ⧀?)

🔷Abstraction āĻšāϞ⧋ OOP-āĻāϰ āĻāĻ•āϟāĻŋ core principle — āϝ⧇āĻ–āĻžāύ⧇ āφāĻŽāϰāĻž āϜāϟāĻŋāϞāϤāĻž (complexity) āϞ⧁āĻ•āĻŋā§Ÿā§‡ āĻĢ⧇āϞāĻŋ āĻāĻŦāĻ‚ āϕ⧇āĻŦāϞāĻŽāĻžāĻ¤ā§āϰ āĻĒā§āĻ°ā§Ÿā§‹āϜāĻ¨ā§€ā§Ÿ āĻŦāĻŋāώ⧟āϗ⧁āϞ⧋ āωāĻĒāĻ¸ā§āĻĨāĻžāĻĒāύ āĻ•āϰāĻŋāĨ¤

👉 āϏāĻšāϜāĻ­āĻžāĻŦ⧇ āĻŦāϞāϞ⧇:

đŸ”ļ “User āϕ⧇ āĻĻāϰāĻ•āĻžāϰāĻŋ āĻ…āĻ‚āĻļ āĻĻ⧇āĻ–āĻžāĻ“, āĻ­āĻŋāϤāϰ⧇āϰ āϜāϟāĻŋāϞāϤāĻž āϞ⧁āĻ•āĻŋā§Ÿā§‡ āϰāĻžāĻ–ā§‹āĨ¤â€

āωāĻĻāĻžāĻšāϰāĻŖ:

āĻāĻ•āϟāĻž āĻ—āĻžā§œāĻŋ āϚāĻžāϞāĻžāϤ⧇ āĻšāϞ⧇ āĻļ⧁āϧ⧁ brake, steering, accelerator āϜāĻžāύāϞ⧇āχ āĻšā§ŸāĨ¤ āĻ­āĻŋāϤāϰ⧇ engine āĻ•āĻŋāĻ­āĻžāĻŦ⧇ āϚāϞ⧇, gearbox āĻ•āĻŋāĻ­āĻžāĻŦ⧇ āĻ•āĻžāϜ āĻ•āϰ⧇ — āĻāϏāĻŦ āϜāĻžāύāĻžāϰ āĻĻāϰāĻ•āĻžāϰ āύ⧇āχāĨ¤

āĻāχ āϜāĻŋāύāĻŋāϏāϟāĻžāχ āĻšāϞ⧋ abstraction — hide the unnecessary, show only what matters.

🔑 What is Abstract? (āĻ…ā§āϝāĻžāĻŦāĻ¸ā§āĻŸā§āϰāĻžāĻ•ā§āϟ āϕ⧀?)

  • 🔷abstract āĻšāϞ⧋ āĻāĻ•āϟāĻž keyword, āϝ⧇āϟāĻž āφāĻŽāϰāĻž TypeScript āĻŦāĻž āĻ…āĻ¨ā§āϝ OOP āĻ­āĻžāώāĻžā§Ÿ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŋ abstraction achieve āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝāĨ¤

  • 🔷abstract class: āĻāĻŽāύ āĻāĻ•āϟāĻž class āϝāĻž āϏāĻŽā§āĻĒā§‚āĻ°ā§āĻŖ āύāĻž — āĻāϟāĻž just āĻāĻ•āϟāĻž blueprintāĨ¤

  • 🔷abstract method: method āφāϛ⧇, āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻāϰ āϕ⧋āύ⧋ body āύ⧇āχāĨ¤ Subclass āĻāϟāĻžāϕ⧇ override āĻ•āϰ⧇ implement āĻ•āϰāĻŦ⧇āĨ¤

đŸ› ī¸ TypeScript āĻĻāĻŋā§Ÿā§‡ Abstract Implement āĻ•āϰāĻž

āφāĻŽāϰāĻž āĻāĻ•āϟāĻž example āĻĻ⧇āĻ–āĻŋ āϝ⧇āĻ–āĻžāύ⧇ abstraction apply āĻ•āϰāĻž āĻšā§Ÿā§‡āϛ⧇ abstract keyword āĻĻāĻŋā§Ÿā§‡āĨ¤

// Abstract class: acts as a blueprint for all shapes
abstract class Shape {
  constructor(private name: string) {}

  public printDetails(): void {
    console.log(`This is a ${this.name} Shape!`);
  }

  // Abstract method: must be implemented by subclasses
  abstract getArea(): number;
}

// Concrete class: Circle extends Shape
class Circle extends Shape {
  constructor(name: string, private radius: number) {
    super(name);
  }

  getArea(): number {
    return Math.PI * this.radius ** 2;
  }
}

// Concrete class: Square extends Shape
class Square extends Shape {
  constructor(name: string, private width: number) {
    super(name);
  }

  getArea(): number {
    return this.width ** 2;
  }
}

// Test instances
const circle = new Circle("Circle", 5);
circle.printDetails(); // This is a Circle Shape!
console.log(circle.getArea()); // 78.53981633974483

const square = new Square("Square", 10);
square.printDetails(); // This is a Square Shape!
console.log(square.getArea()); // 100

💡 āĻŦā§āϝāĻžāĻ–ā§āϝāĻž:

Shape class āϟāĻŋ āĻšāϞ⧋ abstract class — āĻāϤ⧇ getArea() method declare āĻ•āϰāĻž āφāϛ⧇, āĻ•āĻŋāĻ¨ā§āϤ⧁ define āĻ•āϰāĻž āύ⧇āχāĨ¤

Circle, Square āĻšāϞ⧋ subclass āϝ⧇āϟāĻž getArea() āĻāϰ āĻŦāĻžāĻ¸ā§āϤāĻŦ implementation āĻĻāĻŋā§Ÿā§‡āϛ⧇āĨ¤

āĻāχāĻ­āĻžāĻŦ⧇ āφāĻŽāϰāĻž āĻŦāϞāĻ›āĻŋ “āĻĒā§āϰāĻ¤ā§āϝ⧇āĻ• shape-āĻāϰ area āĻĨāĻžāĻ•āĻŦ⧇, āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻ•āĻŋāĻ­āĻžāĻŦ⧇ āϏ⧇āϟāĻž āĻŦ⧇āϰ āĻšāĻŦ⧇ — āϏ⧇āϟāĻž shape āĻ…āύ⧁āϝāĻžā§Ÿā§€ āφāϞāĻžāĻĻāĻž āĻšāĻŦ⧇āĨ¤â€ — āĻāχ āϜāĻŋāύāĻŋāϏāϟāĻžāχ āĻšāϞ⧋ abstractionāĨ¤

đŸŽ¯ How to Achieve Abstraction in OOP?

OOP-āϤ⧇ abstraction achieve āĻ•āϰāĻžāϰ āĻ•āĻŋāϛ⧁ common āωāĻĒāĻžā§Ÿ:

  • ✅ Abstract Class > Design define āĻ•āϰ⧇ āĻĻā§‡ā§Ÿ, implementation subclass āĻ

  • ✅ Interface (in TypeScript) > Structure enforce āĻ•āϰ⧇, implementation āύāĻž

  • ✅ Access Modifiers > private, protected keyword āĻĻāĻŋā§Ÿā§‡ data hide āĻ•āϰāĻž āϝāĻžā§Ÿ

  • ✅ Encapsulation > Internal details āĻāĻ•āϏāĻžāĻĨ⧇ āϞ⧁āĻ•āĻžāύ⧋ āϝāĻžā§Ÿ method/class āĻāϰ āĻŽāĻ§ā§āϝ⧇

đŸ§Ē Summary:

  • 🔸 Abstraction is a core OOP principle that hides internal details and exposes only necessary parts.

  • 🔸 In TypeScript, abstraction is implemented using abstract classes and abstract methods.

  • 🔸 Subclasses must implement those abstract methods, which enforces a contract between base and child classes.

📌 Final Thought:

āφāĻĒāύāĻŋ āϝāĻ–āύ abstraction āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŦ⧇āύ, āϤāĻ–āύ future-proof code āϞāĻŋāĻ–āĻŦ⧇āύ — āϝ⧇āĻ–āĻžāύ⧇ structure āĻĨāĻžāĻ•āĻŦ⧇, āĻ•āĻŋāĻ¨ā§āϤ⧁ implementation āĻĨāĻžāĻ•āĻŦ⧇ flexibleāĨ¤

Abstract ≠ Abstraction, āĻ•āĻŋāĻ¨ā§āϤ⧁ abstract class/method āĻĻāĻŋā§Ÿā§‡āχ abstraction achieve āĻ•āϰāĻž āĻšā§ŸāĨ¤

Comments

  • Type and hit enter to post comment
  • For multiline comments, use Shift + Enter
  • You can use markdown syntax for formatting

Cookie Consent

We use cookies to enhance your browsing experience and analyze our traffic. By clicking "Accept", you consent to our use of cookies.