DEV Community

Discussion on: ELI5: Why cast to an interface?

Collapse
 
hamzaop profile image
Hamza

You can do that for example to check whether some class implements an interface at runtime with a try catch block, if the cast fails then you can throw an error saying that the class should implement the interface, one example is to make a callback between Activity and Fragment in Android.

Thread Thread
 
lluismf profile image
Lluís Josep Martínez

This would be a programming error and you shouldn't use the Exception mechanism to catch errors.