Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why i couldn't do downcasting here

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why am i getting an classcastexception where i could do a downcasting?
am i missing something conceptually?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are creating a new A, which is not a B. B extends A, so a B is an A, but not vice versa.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, using A and B as names is not doing you any favors. Use better examples.

For example, let's make A an Animal and B a Buffalo.

When you create a new Animal you cannot cast it to Buffalo, because we don't know what type of Animal it is. It could be a Bat, or a Baboon, or even a Bear.

Clear?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:why am i getting an classcastexception where i could do a downcasting?
am i missing something conceptually?

Because A IS-NOT-A B (B IS-A A).

In this thread you'll find a bunch of different casts together with (excellent) explanations about a very similar code snippet. And in this thread you'll find a detailed explanation when a cast results in a compiler error or a runtime exception. When you are casting to an interface, it's a little bit trickier. But don't worry, this thread has a very good explanation as well.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:By the way, using A and B as names is not doing you any favors. Use better examples.

Although that's definitely true, certainly when you are learning the different concepts. It's a good thing to have a few exercises with not so meaningful class names as well, because on the actual exam you might encounter a few questions about casting, IS-A and HAS-A relationships using class names like Hmpf, Zing, Quizel, and so on.
 
Liutauras Vilda
Bartender
Pie
Posts: 2756
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is maybe where you got confused.
You can cast reference of super type to sub type, if it were instantiated as a sub class object (this sounds probably confusing, might someone will rephrase me).
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:By the way, using A and B as names is not doing you any favors. Use better examples.

For example, let's make A an Animal and B a Buffalo.

When you create a new Animal you cannot cast it to Buffalo, because we don't know what type of Animal it is. It could be a Bat, or a Baboon, or even a Bear.

Clear?



if so,
upcating can done for subclass type to a superclass type - no special criteria
downcasting can be done only if the the superclass type reference is instanceof subcasstype.
correct me if i'm wrong anywhere.
 
salvin francis
Bartender
Posts: 1335
11
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see if this code solves your understanding

 
salvin francis
Bartender
Posts: 1335
11
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example for you :

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:You can cast reference of super type to sub type, if it were instantiated as a sub class object (this sounds probably confusing, might someone will rephrase me).

Let's give it a try with my best attempt

If both classes belong to a different class hierarchy (so there's no supertype-subtype relationship, like Cat and Dog; String and Integer; Cat and String;...) you'll get a compiler error if you try to cast. If both classes belong to the same class hierarchy (so a supertype-subtype relationship exists, like Object, Animal and Cat; Object, Throwable, Exception, RuntimeException and IllegalArgumentException;...) you won't get a compiler error, but you risk a runtime ClassCastException when the type of the cast and the actual object are not compatible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic