Hi,
arrB is a subclass of arrA,so why shold it not throw a ClassCastException in line 2 itself.
What is the difference betwen lines 2 and 3.
please clear this difference.
thanks
charu.
Originally posted by niraj singh:
Hi charu,
You have not defined the type of variable arr in line 3. I expect you meant it to be arrB. In that case, line 3 will give a ClassCastException. That is because you are assigning a superclass array object to a subclass field.
For example, if A is the superclass and B is the subclass, you can write
A a = new B();
but you can't write, B b = new A();