First: As Ananth said: Line 8 will not compile. Reason: Redeclaration of variable 'o';
Second (and more important one): Why does line 9 gives an error? Reason: You have declared variable 'a' of type A and not A! That is the array of class B cannot be referred by a non-array variable. It will compile fine if you try this: A a = new B;
Now, the million dollar question.... Then how come line 8 executes fine (ofcourse if you change 'o' to say 'ob')? You see, Object is superclass of all objects created in Java and it holds true for array variables too... Thus, when you create array of class B, class Object being the superclass of all arrays, will be able to refer it. Thats it!
SCJP 6 failed - 48% - 28/06/2008
"In great attempts, it is glorious even to fail. - Cassius Longinus"
posted 12 years ago
thanks ananth..i have rectified that. the error given is "incompatible types". i thought that both Object and A are super to B,so,acording to me both should compile. please tell me where am i wrong. [ August 23, 2008: Message edited by: ankana mukherjee ]
Object o=new B; In the above line you are assigning "array of class B obects" into an Object class. Which will easily be done withour errors as array itself is an object and you are asigning abject into an Object class. Basically, new B is an array abject not an abject of type B.
A a=new B; In this line you are assigning an object into a reference of type class A, which obviously is not possible. So, it generates an error.
SCJP 5.0 93%<br /> <br />SCWCD 5.0 97 %
A magnificient life is loaded with tough challenges. En garde tiny ad: