• 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

Java upcasting and downcasting rule for object and Generics?

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

is the Java upcating and downcasting rules are same for general object type or generics types?

1) Dog dog = new Animal();

Type mismatch can't covert Dog to Animal - complie time error

2) Animal animal = new Animal();
Dog dog = (Dog) animal;

java.lang.ClassCastException: Animal cannot be cast to Dog at runtime.

Please comments.

rgds,
Ranjan
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Ranjan wrote:
2) Animal animal = new Animal();
Dog dog = (Dog) animal;

java.lang.ClassCastException: Animal cannot be cast to Dog at runtime.

Please comments.



Can you elaborate -- what do you expect to happen here?

Henry
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks , i mean to say we should not downcast if in this case Animal is not a dog.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Ranjan wrote:Thanks , i mean to say we should not downcast if in this case Animal is not a dog.



Well, yeah. You can't cast an object into a type that it is not.

Henry
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Ranjan wrote:is the Java upcating and downcasting rules are same for general object type or generics types?


Like Henry, I'm not sure what you mean.

Generic types are used to avoid casting of ANY kind - down or up.

Winston
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about if we use the generics types and apss the child class instead of Parent type Animal was defined

method(Dog);

instead of the method argument was initally <Animal>

what will happen?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, if you are going to show example source about a generic, it would probably be a good idea to show us the code for the generic.

Second, if a generic is defined for Animal, then it is used for Animal classes. And since, a Dog class IS-A Animal class (again, assuming, since you never show us the code), then it should work.

Henry
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Ranjan wrote:doSomething(dogs); // problem of line as do Someting is of type Animal list not allowed to pass Dog.


And that's because a List<Animal> is NOT a List<Dog> (or even a superclass of it).

You could change the definition to:

  static void doSomething(List<? extends Animal> animals) { ...

but then you won't be allowed to update the List inside the method. And for the reasons why, you should read the tutorials.

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic