Bird
0
0

Consider these classes:

hard📝 Application Q8 of 15
Java - Polymorphism
Consider these classes:
class Employee { void work() { System.out.println("Employee working"); } } class Manager extends Employee { void work() { System.out.println("Manager managing"); } } class Developer extends Employee { void work() { System.out.println("Developer coding"); } }
How can runtime polymorphism be used to execute the work() method for different employee types?
ABy creating an Employee reference and assigning it to Manager or Developer objects and calling work()
BBy calling static methods directly from Employee class
CBy overloading the work() method with different parameters
DBy using final methods in Employee class
Step-by-Step Solution
Solution:
  1. Step 1: Understand runtime polymorphism

    It allows a superclass reference to point to subclass objects and invoke overridden methods.
  2. Step 2: Apply to given classes

    Using Employee reference to refer to Manager or Developer objects and calling work() will invoke the overridden method of the actual object.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Superclass reference with subclass object calls overridden method [OK]
Quick Trick: Superclass reference calls subclass overridden method [OK]
Common Mistakes:
  • Using static methods which do not support polymorphism
  • Confusing overloading with overriding
  • Using final methods which cannot be overridden

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes