I was asked in an interview to give an example of a ClassCastException for which I gave the below example
class X{}
class Y extends X{}
class Z extends X{}
Y y=new Y();
Z z=new z();
(X)y
is possible
(X)z
is possible
Z(y)
and (Y)z
will throw a ClassCastException.
Then the interview asked me to give real world example for which I said both boat and table are made up of wood
table extends wood boat extends wood but you can not use table for fishing similarily you can not use table to sir or stand
I want to know if my examples are right or not?