• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics Question

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith,
And for you! specially for making me clear on the Line 5 issue!
I got why runtime "ClassCastException" there!



Regards,
cmbhatt
reply
    Bookmark Topic Watch Topic
  • New Topic