BindingProvider bp = (BindingProvider) stub;
This is a narrowing reference conversion. According to one of the rules of Narrowing reference conversion, an interface type K
can be assigned to a non-parameterized interface type J
, provided K
is not a sub-type of J
(you wouldn't require an explicit cast if K
were a sub-type of J
).
J j = (J) K;
If the cast fails, a ClassCastException is thrown at runtime.
InputStream in = System.in;
Runnable r = (Runnable) in;
The above snippet compiles because both InputStream
and Runnable
are non-parameterized interfaces, but would result in a ClassCastException
at run-time.
A cast from ServiceInterface
to BindingProvider
works because getPort
returns a dynamic proxy class that implements the WSBindingProvider
interface, which in turn extends the BindingProvider
interface.