0
0
Javaprogramming~20 mins

Why polymorphism is needed in Java - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Polymorphism Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use polymorphism in Java?

Which of the following best explains why polymorphism is needed in Java programming?

AIt allows objects of different classes to be treated as objects of a common superclass, enabling flexible and reusable code.
BIt forces all classes to have the same methods, even if they do not make sense for some classes.
CIt prevents inheritance and method overriding to keep code simple.
DIt requires all methods to be static so they can be called without creating objects.
Attempts:
2 left
πŸ’‘ Hint

Think about how polymorphism helps when you want to write code that works with many types of objects in a uniform way.

❓ Predict Output
intermediate
2:00remaining
Output of polymorphic method call

What is the output of the following Java code?

Java
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
AAnimal sound
BCompilation error
CBark
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Remember that the actual method called depends on the object's runtime type, not the reference type.

πŸ”§ Debug
advanced
2:00remaining
Identify the polymorphism misuse causing error

What error will this Java code produce and why?

Java
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void sound(int volume) {
        System.out.println("Bark at volume " + volume);
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
ACompilation error: method sound() not found in Dog
BRuntime error: method sound() not found
COutput: Bark at volume 5
DOutput: Animal sound
Attempts:
2 left
πŸ’‘ Hint

Check if the Dog class actually overrides the sound() method with no parameters.

πŸ“ Syntax
advanced
2:00remaining
Which code snippet correctly demonstrates polymorphism?

Which of the following Java code snippets correctly uses polymorphism to call the overridden method?

AAnimal a = new Animal(); a.sound();
BAnimal a = new Dog(); a.sound();
CDog d = new Dog(); d.sound(5);
DDog d = new Animal(); d.sound();
Attempts:
2 left
πŸ’‘ Hint

Remember polymorphism works when a superclass reference points to a subclass object.

πŸš€ Application
expert
3:00remaining
How polymorphism improves code extensibility

Consider a program that processes different types of payment methods: CreditCard, PayPal, and Bitcoin. Which statement best explains how polymorphism helps in extending this program?

ABy allowing new payment classes to be added without changing existing code that processes payments, as long as they implement a common interface.
BBy forcing all payment classes to share the same data fields, making the code less flexible.
CBy requiring the program to know the exact type of each payment method before processing it.
DBy preventing the use of interfaces and abstract classes to simplify the design.
Attempts:
2 left
πŸ’‘ Hint

Think about how polymorphism supports adding new types without modifying existing code.