• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java upcasting and downcasting rule for object and Generics?  RSS feed

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!