casting - Upcasting in java -
In Java, it seems that I have 3 classes, spread from C.B, which extends from A.
"text-post" itemprop = "text" Code> Class X {Interface A {} Interface B extends A {} Interface C Extended B {} Zero F (AA) {} Zero Test () {CC = New C () BB = (B) C ; F (b); }}
If I'm shown in test () , as it looks like:
cc = new C () BB = (B) C; F (b); f () accepts b type c to c And both B have been extended from A . I do not want f () to get it as b type B C . .
Is it anyway offensive?
f () (always in spite of the cover of a B or C ) And some may well downstream.
You can define an additional F () such as
f (bb); and if necessary
f (c c); And the correct one will be said on the basis of the class of logic. That is, the compiler determines the type of argument it is called on, it is different from dynamic dispatch (or polymorphism) which will occur on runtime.
Note that your cast in question is unnecessary. You can write:
cc = new c () bb = c; F (b); Since C is spread from CB, C is a B.