Skip to content

Commit

Permalink
Learned about Polymorphism in OOP and understood the concept of metho…
Browse files Browse the repository at this point in the history
…d overriding
  • Loading branch information
sohail019 committed Jul 28, 2024
1 parent 76af09f commit 42d060c
Showing 1 changed file with 33 additions and 0 deletions.
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)

0 comments on commit 42d060c

Please sign in to comment.