Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #9138
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Why can't I downcast in the following code |
| Date | 2011-10-23 23:17 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <6963560.1010.1319437079999.JavaMail.geo-discussion-forums@yqgd7> (permalink) |
| References | <2196c265-94c4-4fcb-beb5-ddf3a7135da8@l10g2000pra.googlegroups.com> |
Chad wrote:
> Given the following...
>
> class X {}
> class Y extends X {}
> class Z extends X {}
>
> public class Main {
>
> public static void main(String[] args) {
> X x = new X();
> Y y = new Y();
> Z z = new Z();
>
> Y o6 = (Y) x; //<--error
> }
>
> }
>
> I get this error...
>
> run:
> Exception in thread "main" java.lang.ClassCastException: X cannot be
> cast to Y
> at Main.main(Main.java:12)
> Java Result: 1
> BUILD SUCCESSFUL (total time: 0 seconds)
>
>
> I thought I could cast X to Y since they inherited. Why doesn't it
> work in this case?
Inheritance, as you have discovered, does not automatically allow casting.
Upcasting works as you describe; it's downcasting that is tricky. (In Java, an "upcast" is from an inheriting or descendant type to an ancestor type, a "downcast" is from a base or ancestor type to a descendant type.)
To understand this, remember that inheritance (both the 'extends' and the 'implements' variety) represents /is-a/. A descendant thingie /is-an/ ancestor thingie, so in your example,
> class X {}
> class Y extends X {}
> class Z extends X {}
an instance of a 'Y' /is-an/ 'X', but not necessarily would every 'X' /be-a/ 'Y'.
An instance of a 'Z' /is-an/ 'X', but not necessarily would every 'X' /be-a/ 'Z'.
In no way can you say a 'Y' /is-a/ 'Z', nor that a 'Z' /is-a/ 'Y'. Neither one is the parent of the other.
Which is exactly why downcasting is tricky. If you cast an 'X' down to a 'Y', that might actually be a 'Z' you're trying to cast. Oops - 'ClassCastException'. You cannot cast a 'Z' to a 'Y'.
In your case it's worse. You already know that 'x' /is-not-a/ 'Y'. It's a plain old 'X', with nothing of a 'Y' about it. So when you try to cast it down to a 'Y', kaboom!
And by the way, you don't get an error for that, you get an exception for that.
--
Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Why can't I downcast in the following code Chad <cdalten@gmail.com> - 2011-10-23 18:22 -0700
Re: Why can't I downcast in the following code Chad <cdalten@gmail.com> - 2011-10-23 18:47 -0700
Re: Why can't I downcast in the following code Travers Naran <tnaran@gmail.com> - 2011-10-24 07:57 -0700
Re: Why can't I downcast in the following code Roedy Green <see_website@mindprod.com.invalid> - 2011-10-23 18:50 -0700
Re: Why can't I downcast in the following code Chad <cdalten@gmail.com> - 2011-10-23 18:57 -0700
Re: Why can't I downcast in the following code Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-10-23 19:00 -0700
Re: Why can't I downcast in the following code Lew <lewbloch@gmail.com> - 2011-10-23 23:22 -0700
Re: Why can't I downcast in the following code Lew <lewbloch@gmail.com> - 2011-10-23 23:27 -0700
Re: Why can't I downcast in the following code Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-10-23 19:03 -0700
Re: Why can't I downcast in the following code Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-23 23:16 -0400
Re: Why can't I downcast in the following code Donkey Hottie <donkey@fredriksson.dy.fi> - 2011-10-24 07:19 +0300
Re: Why can't I downcast in the following code Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-10-24 00:08 -0500
Re: Why can't I downcast in the following code Lew <lewbloch@gmail.com> - 2011-10-23 23:17 -0700
Re: Why can't I downcast in the following code Lew <lewbloch@gmail.com> - 2011-10-23 23:19 -0700
csiph-web