Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #9133
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Why can't I downcast in the following code |
| Date | Sun, 23 Oct 2011 23:16:39 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 55 |
| Message-ID | <j82las$i9t$1@dont-email.me> (permalink) |
| References | <2196c265-94c4-4fcb-beb5-ddf3a7135da8@l10g2000pra.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Mon, 24 Oct 2011 03:16:44 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="f8igmItKsWs6nM5YanFxAA"; logging-data="18749"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/9CN78EQF423tOLNqKNR7P" |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 |
| In-Reply-To | <2196c265-94c4-4fcb-beb5-ddf3a7135da8@l10g2000pra.googlegroups.com> |
| Cancel-Lock | sha1:El/GgObbMYemqUHO9kDmvVJC0Wg= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9133 |
Show key headers only | View raw
On 10/23/2011 9:22 PM, Chad wrote:
> Given the following...
>
> class X {}
class Automobile {}
> class Y extends X {}
class Toyota extends Automobile {}
> class Z extends X {}
class Saab extends Automobile {}
> public class Main {
>
> public static void main(String[] args) {
> X x = new X();
Automobile x = new Automobile();
> Y y = new Y();
Toyota y = new Toyota();
> Z z = new Z();
Saab z = new Saab();
>
> Y o6 = (Y) x; //<--error
Toyota o6 = (Toyota) x;
> }
>
> }
>
> I get this error...
>
> run:
> Exception in thread "main" java.lang.ClassCastException: X cannot be
> cast to Y
Exception ... Automobile cannot be cast to Toyota
> I thought I could cast X to Y since they inherited. Why doesn't it
> work in this case?
Because all Toyotas are Automobiles, but not all Automobiles
are Toyotas.
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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 Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 16:03 -0500
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