• 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

upcasting and downcasting?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic