Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9145

Re: Why can't I downcast in the following code

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Travers Naran <tnaran@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Why can't I downcast in the following code
Date Mon, 24 Oct 2011 07:57:59 -0700
Organization A noiseless patient Spider
Lines 15
Message-ID <j83udp$opg$1@dont-email.me> (permalink)
References <2196c265-94c4-4fcb-beb5-ddf3a7135da8@l10g2000pra.googlegroups.com> <7b2d0001-bb27-4ea2-aba3-a23b5fca59af@h23g2000pra.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 14:58:01 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="tyezmVsV8/Iz8tdpI+Szpw"; logging-data="25392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18REf0QZSo9yU43z8hnGG8W"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1
In-Reply-To <7b2d0001-bb27-4ea2-aba3-a23b5fca59af@h23g2000pra.googlegroups.com>
Cancel-Lock sha1:ePpX0Irglzp6g6j8iHc3Lfrk7dw=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9145

Show key headers only | View raw


On 23/10/2011 6:47 PM, Chad wrote:
>> I thought I could cast X to Y since they inherited. Why doesn't it
>> work in this case?
>>
>
> I thought I could cast X to Y since *they are* inherited. Why doesn't
> it work in this case?

The simple answer, Chad, is that x (the variable) is NOT Y (the class). 
  You can cast a variable to the actual class it is on up.  You cannot 
turn a parent class into it's child class via casting.

Left as an exercise for the student why this is so.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


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 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