java interface references downcasting -
interface {} interface b{} interface c{} a = new a(){}; b b = (b)a; c c = (c)b;
even though these interfaces not related , above code seems compile ,if change these interfaces classes , doesn't compile obvious reasons . why interfaces behave way
the interfaces unrelated, there classes implement 3 interfaces, there instances can cast around merrily.
the reason not work classes given class can extend 1 superclass, there cannot class extends both class a
, class b
.
another spin on final
classes. cannot cast, example, string
map
because there can no subclasses of string
implement map
. can cast date
or arraylist
map
because there subclasses fit.
note reasoning applies @ compile-time. invalid cast still fail @ run-time. compile-time type-checking supposed catch invalid casts as possible, not cases can detected static type checking (at least not in java). unless compiler can guarantee cast makes no sense, allow it.
Comments
Post a Comment