• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can i have a practical example for down casting in java?

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why down casting is needed in java. Can i have a practical example for down casting in java?. I please do not want an academic example.
 
Marshal
Posts: 80942
521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is something you should avoid; lots of casts means your classes are designed badly. Look at this method, which isn't in the List interface. If you want to use it, you have to do some horrible testing with instanceof to prevent class cast exceptions:-LinkedList doesn't have such a method because the size of a linked list and its capacity are always the same. I shall leave you to work out how to handle such a method in this List implementation.
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only two things I can think of that I use are casting Object to X in my X.equals(Object) implementation, and casting a Graphics reference to a Graphics2D reference when I'm overriding JComponent.paintComponent(Graphics).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic