JAVA SP T


Lets dive into snapshot of my programs!💻

14.Create a program, showing an example of dynamic method dispatch

💡Code:

 class Animal {
              void makeSound() {
              System.out.println("The animal makes a sound.");
              }
              }
              
              class Dog extends Animal {
              void makeSound() {
              System.out.println("The dog barks.");
              }
              }
              
              class Cat extends Animal {
              void makeSound() {
              System.out.println("The cat meows.");
              }
              }
              
              class test {
              public static void main(String[] args) {
              Animal myAnimal;
              myAnimal = new Dog();
              myAnimal.makeSound();
              
              
              myAnimal = new Cat();
              myAnimal.makeSound();
              
              }
              }

📸Output :

output