0
0
Javaprogramming~10 mins

Runtime polymorphism in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a method that can be overridden.

Java
public class Animal {
    public void [1]() {
        System.out.println("Animal sound");
    }
}
Drag options to blanks, or click blank then click option'
AmakeSound
Bspeak
Csound
Dnoise
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a method name that is not descriptive or inconsistent with subclass methods.
2fill in blank
medium

Complete the code to override the method in the subclass.

Java
public class Dog extends Animal {
    @Override
    public void [1]() {
        System.out.println("Bark");
    }
}
Drag options to blanks, or click blank then click option'
AmakeSound
Bnoise
Cspeak
Dsound
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different method name that does not override the superclass method.
3fill in blank
hard

Fix the error in the code to call the overridden method at runtime.

Java
Animal myAnimal = new Dog();
myAnimal.[1]();
Drag options to blanks, or click blank then click option'
Asound
BmakeSound
Cspeak
Dnoise
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Calling a method name that does not exist in the superclass reference.
4fill in blank
hard

Fill both blanks to complete the code that demonstrates runtime polymorphism with two subclasses.

Java
Animal myAnimal1 = new Dog();
Animal myAnimal2 = new Cat();
myAnimal1.[1]();
myAnimal2.[2]();
Drag options to blanks, or click blank then click option'
AmakeSound
Bsound
Cspeak
Dnoise
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different method names for each call that do not exist in the superclass.
5fill in blank
hard

Fill all three blanks to complete the code that uses runtime polymorphism with method overriding and dynamic method dispatch.

Java
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]();
    }
}
Drag options to blanks, or click blank then click option'
AmakeSound
Bsound
Cspeak
Dnoise
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different method names in declaration, override, or call causing compile errors.