Casting to an interface allows you to pass/refer to objects with the same behavior.
For example, if you wanted to make a report from a bunch of forecasts you could write code like this:
/* Collect some forecastable things */List<Forecastable>forecastables=Arrays.asList(nycWeather,frankfurtWeather,nyse,dax);/* Get the forecasts themselves */List<Forecast>forecasts=forecastables.stream().map(Forecastable::getForecast).collect(Collectors.toList());/* Add the forecasts to our report */report.addForecasts(forecasts);
Here you can see that we only need the Forecastable subset of these objects' functionality so we cast them to the interface.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Casting to an interface allows you to pass/refer to objects with the same behavior.
For example, if you wanted to make a report from a bunch of forecasts you could write code like this:
Here you can see that we only need the
Forecastable
subset of these objects' functionality so we cast them to the interface.