diff --git a/03 - Chai aur Javascript/12 - Object Oriented Programming/05_polymorphism.js b/03 - Chai aur Javascript/12 - Object Oriented Programming/05_polymorphism.js new file mode 100644 index 0000000..4f027d2 --- /dev/null +++ b/03 - Chai aur Javascript/12 - Object Oriented Programming/05_polymorphism.js @@ -0,0 +1,33 @@ +// Polymorphism + +class Animal{ + // ye class mein generic method makeSound() define karenge + makeSound(){ + console.log("Animal Makes a Sound."); + } +} + +// Child classes mein Animal class ko extend karenge aur makeSound() method ko apni specific implementations ke sath override karenge + +class Dog extends Animal{ + makeSound(){ + console.log("Dog Makes Sound of Bow! Bow!") + } +} + +class Cat extends Animal{ + makeSound(){ + console.log("Cat Makes Sound of Meow! Meow!"); + } +} + +// ek function banaenge jo generic interface provide karega jo kisi bhi animal ke object ke liye makeSound() method ko call karega +function animalSound(animal){ + animal.makeSound(); +} + +const dog = new Dog(); +const cat = new Cat(); + +animalSound(dog) +animalSound(cat) \ No newline at end of file