Complete the code to declare a method that can be overridden.
public class Animal { public void [1]() { System.out.println("Animal sound"); } }
The method name makeSound is used to demonstrate runtime polymorphism by overriding it in subclasses.
Complete the code to override the method in the subclass.
public class Dog extends Animal { @Override public void [1]() { System.out.println("Bark"); } }
The subclass Dog overrides the makeSound method to provide its own implementation.
Fix the error in the code to call the overridden method at runtime.
Animal myAnimal = new Dog();
myAnimal.[1]();Calling makeSound() on the Animal reference invokes the overridden method in Dog at runtime.
Fill both blanks to complete the code that demonstrates runtime polymorphism with two subclasses.
Animal myAnimal1 = new Dog(); Animal myAnimal2 = new Cat(); myAnimal1.[1](); myAnimal2.[2]();
Both Dog and Cat override the makeSound() method, so calling it on Animal references demonstrates runtime polymorphism.
Fill all three blanks to complete the code that uses runtime polymorphism with method overriding and dynamic method dispatch.
class Animal { public void [1]() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override public void [2]() { System.out.println("Bark"); } } public class Test { public static void main(String[] args) { Animal myAnimal = new Dog(); myAnimal.[3](); } }
The method makeSound() is declared in Animal, overridden in Dog, and called on an Animal reference holding a Dog object to demonstrate runtime polymorphism.