I can understand what upcasting is but downcasting is a little confusing. My question is why should we downcast? Can you help me with a real world example ? Is downcasting that important?
I can understand what upcasting is but downcasting is a little confusing. My question
Share
Downcasting is a necessary evil, for example when dealing with legacy APIs that return non-generic collections. Another classic example is an equals method:
I can’t think of an example where Upcasting is necessary though. OK, it’s good practice to return the least specific possible Object from a method, but that can be done entirely without casting: