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


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

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail
NNTP-Posting-Date Sun, 23 Oct 2011 21:03:23 -0500
Date Sun, 23 Oct 2011 19:03:21 -0700
From Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1
MIME-Version 1.0
Newsgroups comp.lang.java.programmer
Subject Re: Why can't I downcast in the following code
References <2196c265-94c4-4fcb-beb5-ddf3a7135da8@l10g2000pra.googlegroups.com>
In-Reply-To <2196c265-94c4-4fcb-beb5-ddf3a7135da8@l10g2000pra.googlegroups.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Message-ID <mY2dnWeeMpD2WjnTnZ2dnUVZ_g2dnZ2d@posted.palinacquisition> (permalink)
Lines 32
X-Usenet-Provider http://www.giganews.com
NNTP-Posting-Host 50.46.118.188
X-Trace sv3-MmrNI+hL29/k+I+emcDLeSqlPdEw8USg4RiejpBgsZ7WvRnrt6REPAfxZXwiu5GqJzUv/LjSrQFCvFG!hWeplgXQcFnGefgyk8Thyj4XbN1cKu+Qejb9e4uUcb/GWuigzMKweV6VKkyyPT44AKi01aqNWcrI!VHkxYQg4nglrrR3HgHNwX4wzK+XO2h+DBents844dxA=
X-Complaints-To abuse@iinet.com
X-DMCA-Complaints-To abuse@iinet.com
X-Abuse-and-DMCA-Info Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info Otherwise we will be unable to process your complaint properly
X-Postfilter 1.3.40
X-Original-Bytes 2369
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9131

Show key headers only | View raw


On 10/23/11 6:22 PM, Chad wrote:
> [...]
> 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?

Because the object referenced by the variable "x" is not an instance of 
Y.  It's an instance of X.

The following would work:

   X x = new Y();
   Y y = (Y) x;

Casting only allows a change of the way an object is viewed by the code. 
  An instance of Y referenced by a variable of type X can only be 
accessed via members of X.  But it's still a Y and the reference can be 
cast to an expression of type Y.

But if the object isn't a Y in the first place, it's not allowed to be 
referenced by a variable of type Y.  After all, if that were allowed, 
what would it mean to invoke a member of Y on an object that doesn't 
even implement that member?

Pete

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