• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
joshua antony
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


We do not get ClassCastException at runtime at here because generics is not applicable at runtime. Am I right?
[ October 19, 2006: Message edited by: Barry Gaunt ]
 
Petrus Pelser
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may not get an exception at runtime, but you would not be able call a Circle (Compiler complains) or Rectangle (Runtime exception) specific method on an object in the list.
 
Aniket Patil
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList<Circle> a = new ArrayList<Circle>();
ArrayList b = a;
ArrayList<Rectangle> c = b //Here

Take a look at the change at "Here". It works even without the cast, to ensure compatibility with older versions.
 
Sharmila Rishi
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aniket,
According to your code


what you are implying is


how will it compile?
can you please explain.



-Sharmila Rishi.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sharmila Rishi:
Hi Aniket,
According to your code


what you are implying is


how will it compile?
can you please explain.



-Sharmila Rishi.


The type information is lost when the raw ArrayList reference b is assigned to point to the ArrayList<Circle> created in the first line.
 
Sharmila Rishi
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the result would be



Am i right?
If yes, then can we add elements to the ArrayList..?

What will happen when the elements are read back?

Should we have to type cast it...?



-Sharmila Rishi.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic