-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about Polymorphism in OOP and understood the concept of metho…
…d overriding
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
03 - Chai aur Javascript/12 - Object Oriented Programming/05_polymorphism.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |