Upcasting and Downcasting in java

后端 未结 8 1663
孤城傲影
孤城傲影 2021-01-13 21:26

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 time example ? Is downcasting th

8条回答
  •  無奈伤痛
    2021-01-13 21:44

    To access the header methods of of the ServletResponse in a filter, you have to downcast to to an HttpServletResponse.

    It's good to declare objects by the class they extend, so you can change the implementation on the fly. However if you need to access any of the methods that are specific to the implementation, you need to downcast.

提交回复
热议问题