casting objects
1 posts in topic
Flat View  Flat View

Posted By:   viswabharathy_kunapuli
Posted On:   Friday, February 1, 2002 08:59 PM

Where does the need come to cast a superclass reference to a subclass object?

Re: casting objects

Posted By:   Scott_McKinney  
Posted On:   Friday, February 1, 2002 09:51 PM

All the time in Java.

For example whenever you use any of the Collection classes you'll need to downcast (aka narrow cast) the items in the collection which are Objects e.g.,

ArrayList al = new ArrayList();

ArrayList.add( new Integer( 42 ) );

Integer item = (Integer)al.get( 0 );

About | Sitemap | Contact