Bird
0
0

Why is upcasting considered safe in Java but downcasting can cause runtime errors?

hard📝 Conceptual Q10 of 15
Java - Polymorphism
Why is upcasting considered safe in Java but downcasting can cause runtime errors?
AUpcasting is implicit and always valid; downcasting requires explicit cast and may fail if object type mismatches.
BUpcasting narrows the type, downcasting widens it.
CDowncasting is implicit and always valid; upcasting requires explicit cast.
DBoth upcasting and downcasting always cause runtime errors.
Step-by-Step Solution
Solution:
  1. Step 1: Understand upcasting behavior

    Upcasting is implicit and safe because subclass is always a type of superclass.
  2. Step 2: Understand downcasting risks

    Downcasting requires explicit cast and can fail if the actual object is not of the subclass type.
  3. Final Answer:

    Upcasting is implicit and always valid; downcasting requires explicit cast and may fail if object type mismatches. -> Option A
  4. Quick Check:

    Upcasting safe implicit; downcasting risky explicit [OK]
Quick Trick: Upcasting safe implicit; downcasting risky explicit cast [OK]
Common Mistakes:
  • Thinking downcasting is always safe
  • Confusing implicit and explicit casts
  • Assuming upcasting can cause runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes