• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

why i couldn't do downcasting here

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


why am i getting an classcastexception where i could do a downcasting?
am i missing something conceptually?
 
Sheriff
Posts: 67663
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67663
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8381
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets see if this code solves your understanding

 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another example for you :

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What's that smell? Hey, sniff this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic