• Post Reply Bookmark Topic Watch Topic
  • New Topic

upcasting and downcasting?  RSS feed

 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my own knowledge I am trying to differentiate upcasting and downcasting. I understand upcasting and can present it in an easy example like this:

Shape = Parent class
circle = subclass
square = subclass

so then you could write a method in the parent class that would look like this: public Shape draw (Shape s){} and since circle and sqaure contain all of shapes methods through inheritance they will be able to be passed.This is upcasting and upcasting is availible to you due to late binding i belive. oh yea and this example is considered upcasting to the parent from a child i belive.

So my question is could someone provide an uncomplicated example of downcasting and tell me if When you retrieve an item from an array of Objects, should you upcast or downcast that item?(using the iterator interface).

Thanks , Kp
[ March 07, 2005: Message edited by: Kev Peterson ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kev,
You are correct in thinking that retrieving an object from a list is downcasting. For example,

The downcast is necessary because a list/iterator contains objects. In Java 1.5 (using generics), this scenario loses value because you can tell the list the specific type.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!