• 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

down cast only throw exception

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what problem with this code?

I have class


but I got class cast exception,

it is a down cast only then why it throw exception
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jacob,

You try to cast an Object instance to an abc instance which is not possible.
Maybe you wanted to write


Anyway it is advised to use uppercase starting letters for class names.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polymorphism support down casting,

My code is as follow



But I got casting exception why, is it valid casting or not ?

Please explain the reaon for this also
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you misunderstood something. I advice you to reread the subject.
Just think about it logically:
Class B extends A. It means that B has everything which A has plus it can have some other methods and fields. So whenever you need an A instance you can use a B instance since it contains everything what is needed for the A instance, but you can't use an A instance as a B instance.
An A type reference can hold an A instance and a B instance. If the reference contains a B instance then you can cast your reference to B. In your example the reference contains an A instance(not a B) so you can't cast it to B.


 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the instanceof operator to to see if the object you're trying to cast is castable (is that a word) to the type you want. It will prevent you from getting the ClassCastException.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple point to remember is

Super class reference variable can point to Sub class instance not vice versa.

Hoe this anwers your question.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic