Share
Facebook
Twitter
LinkedIn
Whatsapp
Instagram
Messenger

জাভাস্ক্রিপ্ট Polymorphism



Polymorphism কি?

Object Oriented Programming-এ Polymorphism বা পলিমরফিজম হলো একই ধরনের interface বা method-এর মাধ্যমে ভিন্ন object-এর ভিন্ন আচরণ প্রকাশ করার ধারণা।

সহজভাবে বললে, একই method name ব্যবহার করা হলেও object অনুযায়ী method-এর কাজ আলাদা হতে পারে। JavaScript-এ inheritance এবং method overriding ব্যবহার করে Polymorphism-এর ধারণা সহজে বোঝানো যায়।

Polymorphism = একই interface, কিন্তু object অনুযায়ী ভিন্ন আচরণ।

উদাহরণ হিসেবে Animal class-এর sound() method বিভিন্ন child class-এ ভিন্ন output দিতে পারে।

উদাহরণ


class Animal {
  sound() {
    return "Animal makes a sound";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

const animal = new Animal();
const dog = new Dog();

console.log(animal.sound());
console.log(dog.sound());

কোড এডিটর



Method Overriding কী?

Inheritance ব্যবহার করার সময় child class তার parent class-এর কোনো method নিজের প্রয়োজন অনুযায়ী নতুনভাবে define করলে তাকে Method Overriding বলা হয়।

Polymorphism বোঝার জন্য method overriding একটি গুরুত্বপূর্ণ ধারণা। Parent class-এ একটি common method রাখা যায় এবং child class সেই method-এর আলাদা implementation দিতে পারে।

Method Overriding = Parent method-কে Child class-এ নতুন implementation দেওয়া।

নিচের উদাহরণে Animal class-এর sound() method Dog class-এ override করা হয়েছে।

উদাহরণ


class Animal {
  sound() {
    return "Animal sound";
  }
}

class Dog extends Animal {
  sound() {
    return "Woof!";
  }
}

const dog = new Dog();
console.log(dog.sound());

কোড এডিটর



একাধিক Child Class-এ Polymorphism

একটি parent class-এর common method বিভিন্ন child class-এ আলাদাভাবে implement করা যায়। ফলে একই method call বিভিন্ন object-এর জন্য ভিন্ন result দিতে পারে।

এভাবে application-এ common structure বজায় রেখেও object-এর নিজস্ব behavior তৈরি করা যায়।

একটি common method + বিভিন্ন implementation = Polymorphic Behavior

নিচের উদাহরণে Dog, Cat এবং Cow একই sound() method ব্যবহার করলেও তাদের output আলাদা।

উদাহরণ


class Animal {
  sound() {
    return "Animal sound";
  }
}

class Dog extends Animal {
  sound() {
    return "Woof!";
  }
}

class Cat extends Animal {
  sound() {
    return "Meow!";
  }
}

class Cow extends Animal {
  sound() {
    return "Moo!";
  }
}

console.log(new Dog().sound());
console.log(new Cat().sound());
console.log(new Cow().sound());

কোড এডিটর



Common Interface-এর ধারণা

JavaScript-এ Java বা C#-এর মতো strict interface keyword নেই। তবে একটি common method structure অনুসরণ করে বিভিন্ন class-কে একই ধরনের interface-এর মতো ব্যবহার করা যায়।

এর ফলে calling code জানে কোন method ব্যবহার করতে হবে, কিন্তু প্রতিটি class সেই method কীভাবে কাজ করবে তা নিজের মতো নির্ধারণ করতে পারে।

Common Interface = একই ধরনের method contract, কিন্তু implementation ভিন্ন হতে পারে।

নিচের উদাহরণে Payment-এর বিভিন্ন class pay() method প্রকাশ করছে।

উদাহরণ


class CardPayment {
  pay(amount) {
    return "Paid " + amount + " by card";
  }
}

class CashPayment {
  pay(amount) {
    return "Paid " + amount + " by cash";
  }
}

console.log(new CardPayment().pay(1000));
console.log(new CashPayment().pay(1000));

কোড এডিটর



Polymorphism ও Inheritance

Inheritance parent class-এর property ও method child class-এ reuse করার সুযোগ দেয়। Polymorphism সেই common method-কে child class অনুযায়ী ভিন্ন behavior দিতে সাহায্য করে।

অর্থাৎ inheritance structure তৈরি করতে সাহায্য করে এবং overriding-এর মাধ্যমে polymorphic behavior প্রকাশ করা যায়।

Inheritance তৈরি করে সম্পর্ক; Polymorphism প্রকাশ করে ভিন্ন behavior।

নিচের উদাহরণে Shape parent class এবং Rectangle ও Circle child class।

উদাহরণ


class Shape {
  area() {
    return 0;
  }
}

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

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

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

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

console.log(new Rectangle(10, 5).area());
console.log(new Circle(5).area());

কোড এডিটর



একই Function দিয়ে বিভিন্ন Object

একটি function এমনভাবে লেখা যায় যাতে সেটি নির্দিষ্ট একটি class-এর উপর নির্ভর না করে একটি common method ব্যবহার করে। তখন বিভিন্ন object একই function-এর মাধ্যমে নিজের behavior প্রকাশ করতে পারে।

এটি code reuse এবং flexible design তৈরিতে সাহায্য করে।

একটি function + common method = বিভিন্ন object-এর সঙ্গে কাজ করার সুযোগ।

নিচের উদাহরণে printSound() function যেকোনো object-এর sound() method ব্যবহার করছে।

উদাহরণ


class Dog {
  sound() {
    return "Woof!";
  }
}

class Cat {
  sound() {
    return "Meow!";
  }
}

function printSound(animal) {
  console.log(animal.sound());
}

printSound(new Dog());
printSound(new Cat());

কোড এডিটর



Polymorphism ও super

Child class method override করার পরেও parent class-এর একই method-এর functionality প্রয়োজন হলে super ব্যবহার করা যায়। এতে parent behavior-এর সঙ্গে child-এর অতিরিক্ত behavior যোগ করা সম্ভব।

এটি বিশেষভাবে useful যখন child class পুরোপুরি নতুন behavior না দিয়ে parent behavior-এর উপর ভিত্তি করে কাজ বাড়াতে চায়।

super.methodName() = Parent class-এর method call করার একটি উপায়।

নিচের উদাহরণে Dog class parent sound() method-এর result-এর সঙ্গে নিজের text যোগ করছে।

উদাহরণ


class Animal {
  sound() {
    return "Animal sound";
  }
}

class Dog extends Animal {
  sound() {
    return super.sound() + " and Dog barks";
  }
}

const dog = new Dog();
console.log(dog.sound());

কোড এডিটর



Polymorphism-এর বাস্তব ব্যবহার

বাস্তব application-এ একই কাজের বিভিন্ন implementation থাকলে Polymorphism ব্যবহার করা যেতে পারে। যেমন payment, notification, storage বা report system-এ একই method name-এর মাধ্যমে বিভিন্ন service ব্যবহার করা যায়।

ফলে মূল application logic-কে প্রতিটি implementation-এর internal detail জানার প্রয়োজন কমে যায়।

Common behavior-এর জন্য common method এবং service অনুযায়ী আলাদা implementation ব্যবহার করা যায়।

নিচের উদাহরণে Email ও SMS notification একই send() method প্রকাশ করছে।

উদাহরণ


class EmailNotification {
  send(message) {
    return "Email: " + message;
  }
}

class SMSNotification {
  send(message) {
    return "SMS: " + message;
  }
}

function notify(service, message) {
  console.log(service.send(message));
}

notify(new EmailNotification(), "Hello");
notify(new SMSNotification(), "Hello");

কোড এডিটর



Polymorphism-এর সুবিধা

Polymorphism বড় application-এ flexible এবং reusable code structure তৈরি করতে সাহায্য করতে পারে। একই interface বা method ব্যবহার করে বিভিন্ন implementation পরিচালনা করা সম্ভব হয়।

নতুন implementation যোগ করার সময় existing calling code অপরিবর্তিত রাখা সম্ভব হলে application maintain করা সহজ হতে পারে।

Polymorphism code-কে flexible, reusable এবং extensible করতে সাহায্য করতে পারে।

নিচের উদাহরণে বিভিন্ন payment object একই pay() method-এর মাধ্যমে ব্যবহার করা হচ্ছে।

উদাহরণ


class CardPayment {
  pay(amount) {
    return "Card payment: " + amount;
  }
}

class MobilePayment {
  pay(amount) {
    return "Mobile payment: " + amount;
  }
}

function makePayment(payment, amount) {
  console.log(payment.pay(amount));
}

makePayment(new CardPayment(), 2000);
makePayment(new MobilePayment(), 2000);

কোড এডিটর



Polymorphism, Overriding ও বাস্তব ধারণা

JavaScript-এ Polymorphism বুঝতে হলে common method, inheritance এবং method overriding-এর সম্পর্ক বোঝা গুরুত্বপূর্ণ। একই method name বিভিন্ন object-এ ভিন্ন behavior প্রকাশ করতে পারে।

সব Polymorphism inheritance-এর মাধ্যমে হতে হবে এমন নয়। JavaScript-এর flexible object model-এর কারণে common method থাকা আলাদা object-ও একই function-এর সঙ্গে কাজ করতে পারে।

মনে রাখুন: Polymorphism-এর মূল ধারণা হলো একই ধরনের operation-কে বিভিন্ন object তাদের নিজস্বভাবে সম্পাদন করতে পারে।

নিচের উদাহরণে Dog ও Cat একই sound() method প্রকাশ করছে এবং printSound() function উভয়ের সঙ্গেই কাজ করছে।

উদাহরণ


class Dog {
  sound() {
    return "Woof!";
  }
}

class Cat {
  sound() {
    return "Meow!";
  }
}

function printSound(animal) {
  console.log(animal.sound());
}

printSound(new Dog());
printSound(new Cat());

কোড এডিটর