Generics Question
posted 18 years ago
Source: Java Beat
Please guide me to understand this question!
Regards,
cmbhatt
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Basket b = new Basket(); // 1
Basket<Apple> bA = b; // 2
Basket<Orange> bO = b; // 3
bA.setElement(new Apple()); // 4
Orange orange = bO.getElement(); // 5
a)The lines 2 and 3 will cause a compile error.
b)The line 4 will cause a compile error.
c)The line 5 will cause a compile error because a cast is missing.
d)The source code will be compiled with warning(s). During the runtime a ClassCastException will be thrown in the line 5.
e)The soure code will be compiled with warning(s). No exception will be thrown during the runtime.
Source: Java Beat
Please guide me to understand this question!
Regards,
cmbhatt
cmbhatt
Chandra Bhatt
Ranch Hand
Posts: 1710
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
My two questions (generics) are left orphaned, nobody cares to answer!
Isn't it like Sun has removed the generics from exam
***
Regards,
cmbhatt
***Don't panic please, it is nothing like that. I was eagerly waiting for the answer.
Isn't it like Sun has removed the generics from exam
***Regards,
cmbhatt
***Don't panic please, it is nothing like that. I was eagerly waiting for the answer.
cmbhatt
posted 18 years ago
You code is just like the code above. The code compile with warning and will get runtime exception java.lang.ClassCastException on line 5.
[ April 11, 2007: Message edited by: Sam Sunamin ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You code is just like the code above. The code compile with warning and will get runtime exception java.lang.ClassCastException on line 5.
[ April 11, 2007: Message edited by: Sam Sunamin ]
Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I would say option 5.
Consider these test classes.
In line 1, you create a new instance of a Basket but all type information is erased because you created a raw Basket.
In line 2, you cause a compile-time warning to be generated because you are assigning a type safe reference equal to a raw reference.
You also do the same thing in line 3.
Line 4 is fine because bA has reference type Basket<Apple> so you can add an instance of an Apple to it.
But in line 5, the getElement method called on the reference type Basket<Orange> tries to cast the element in the Basket, which is of type Apple, to an Orange.
So you get a ClassCastException.
Consider these test classes.
In line 1, you create a new instance of a Basket but all type information is erased because you created a raw Basket.
In line 2, you cause a compile-time warning to be generated because you are assigning a type safe reference equal to a raw reference.
You also do the same thing in line 3.
Line 4 is fine because bA has reference type Basket<Apple> so you can add an instance of an Apple to it.
But in line 5, the getElement method called on the reference type Basket<Orange> tries to cast the element in the Basket, which is of type Apple, to an Orange.
So you get a ClassCastException.
Chandra Bhatt
Ranch Hand
Posts: 1710
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Keith,
And
for you! specially for making me clear on the Line 5 issue!
I got why runtime "ClassCastException" there!
Regards,
cmbhatt
And
for you! specially for making me clear on the Line 5 issue!I got why runtime "ClassCastException" there!
Regards,
cmbhatt
cmbhatt
| BWA HA HA HA HA HA HA! Tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









