My question is related to Java language.
This is what I got:
interface I1{}
interface I2{}
class C1 implements I1{}
class C3 extends C1 implements I2{}
When
C1 01;
C3 o3;
I1 i1;
etc
And now it turns out that I2 i2 = (I2) i1; is right because at run time i1 actually refers to an object that implements I2.
But I don’t get it. Interfaces have no relationships between one another, so how can you cast it to an adjacent interface?
There is no more code, it is simply a drill in order to prepare for Java certification.
Best regards
I2 i2 = (I2) i1;means: I know that the concrete runtime type of the object referenced byi1implements theI2interface, so I would like to reference it as anI2. If the concrete runtime type ofi1indeed implementsI2, the cast will succeed.The fact that
I1andI2have nothing in common doesn’t matter. What matters is the actual concrete runtime type of the object referenced byi1.