Bird
0
0

Which of the following Java code snippets correctly demonstrates polymorphism?

easy📝 Syntax Q12 of 15
Java - Polymorphism
Which of the following Java code snippets correctly demonstrates polymorphism?
ADog d = new Animal(); d.sound();
BDog d = new Dog(); d.bark();
CAnimal a = new Animal(); a.sound();
DAnimal a = new Dog(); a.sound();
Step-by-Step Solution
Solution:
  1. Step 1: Check object assignment compatibility

    Polymorphism allows a superclass reference to point to a subclass object, like Animal a = new Dog();
  2. Step 2: Verify method call correctness

    Calling a method on the superclass reference that is overridden in subclass shows polymorphism.
  3. Final Answer:

    Animal a = new Dog(); a.sound(); -> Option D
  4. Quick Check:

    Superclass ref to subclass object = polymorphism [OK]
Quick Trick: Superclass reference can hold subclass object [OK]
Common Mistakes:
  • Assigning superclass object to subclass reference
  • Using subclass-specific methods on superclass reference
  • Ignoring method overriding in polymorphism

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes