• 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

Downcasting gives compiler error.

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my program Downcasting gives compiler error even if I cast it, please see in my below program:


error:

Exception in thread "main" java.lang.ClassCastException: multipleintrfce.sup1 cannot be cast to multipleintrfce.dymnandupcastng
at multipleintrfce.dymnandupcastng.main(dymnandupcastng.java:48)
 
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

sarojni agrwal wrote:In my program Downcasting gives compiler error even if I cast it, please see in my below program:

error:

Exception in thread "main" java.lang.ClassCastException: multipleintrfce.sup1 cannot be cast to multipleintrfce.dymnandupcastng
at multipleintrfce.dymnandupcastng.main(dymnandupcastng.java:48)



While all dymandupcasting objects are sup1 objects, and hence, cast between them... not all sup1 objects are dymnandupcasting objects, and hence, not always can be cast. In your case, your example instantiated a sup1 class instance directly, which can't be cast to a dymnandupcasting type.

Henry
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile.
No compilation error in my system.
 
sarojni agrwal
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what should I do then for downcasting?
 
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

sarojni agrwal wrote:what should I do then for downcasting?



You can only downcast objects that IS-A that subtype. This means that you can downcast a reference to something that was previously upcasted.

Henry
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, rename your classes to conform with Java recommended usages.

Class names should begin with an uppercase letter. Names beginning with lowercase letters are used for instances and properties. Some tools can become confused if you do not conform to this standard.

I have a suspicion that possibly you're doing this exercise under Windows, and something is converting (or not converting) classfile names in accordance with that rule, then failing to recognize the corresponding file. Windows doesn't care about upper/lower case when locating files by name. Java does, even under Windows.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I copy/pasted OP's code and compiled like this. No compilation error.

2 class files got generated.
dymnandupcastng.class and sup1.class
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tapas Chand wrote:I copy/pasted OP's code and compiled like this. No compilation error.

2 class files got generated.
dymnandupcastng.class and sup1.class



Try and run it.

The OP posted an exception, not a compilation error.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic