javaobjectcasting

Java compilation question regarding casting


If I have two objects not related to each other. What will happen in the following cases: We have: Object A, Object B

A a = new A();
B b = new B();
  1. a = b; \will compile? will I get an exception?
  2. a = (A) b; \will compile ? afterwards - will I get an exception?

Solution

  • You can simply compile them to get the answer as well.

    1. No Won't compile

    2. No Won't compile

    See Also