Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #9126 > unrolled thread
| Started by | Chad <cdalten@gmail.com> |
|---|---|
| First post | 2011-10-23 18:22 -0700 |
| Last post | 2011-10-23 23:19 -0700 |
| Articles | 15 — 9 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-23 18:22 -0700 |
| Subject | Why can't I downcast in the following code |
| Message-ID | <2196c265-94c4-4fcb-beb5-ddf3a7135da8@l10g2000pra.googlegroups.com> |
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?
Chad
[toc] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-23 18:47 -0700 |
| Message-ID | <7b2d0001-bb27-4ea2-aba3-a23b5fca59af@h23g2000pra.googlegroups.com> |
| In reply to | #9126 |
> 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?
[toc] | [prev] | [next] | [standalone]
| From | Travers Naran <tnaran@gmail.com> |
|---|---|
| Date | 2011-10-24 07:57 -0700 |
| Message-ID | <j83udp$opg$1@dont-email.me> |
| In reply to | #9127 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-10-23 18:50 -0700 |
| Message-ID | <crg9a7tqoi1daf8ct9itqde4inunovv9rb@4ax.com> |
| In reply to | #9126 |
On Sun, 23 Oct 2011 18:22:38 -0700 (PDT), Chad <cdalten@gmail.com>
wrote, quoted or indirectly quoted someone who said :
> X x = new X();
> Y y = new Y();
> Z z = new Z();
>
> Y o6 = (Y) x; //<--error
x references an X object not a Y. Perhaps the compiler is smart enough
to notice that. Perhaps it knows that cast could never succeed.
I would expect it would be happy with code like this where it does not
know precisely what value x has.
dosomething( X x )
{
Y o6 = (Y)x;`
}
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-10-23 18:57 -0700 |
| Message-ID | <7303478a-1a8a-41f5-8d3a-262be61130fd@v8g2000pro.googlegroups.com> |
| In reply to | #9128 |
On Oct 23, 6:50 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Sun, 23 Oct 2011 18:22:38 -0700 (PDT), Chad <cdal...@gmail.com>
> wrote, quoted or indirectly quoted someone who said :
>
> > X x = new X();
> > Y y = new Y();
> > Z z = new Z();
>
> > Y o6 = (Y) x; //<--error
>
> x references an X object not a Y. Perhaps the compiler is smart enough
> to notice that. Perhaps it knows that cast could never succeed.
>
> I would expect it would be happy with code like this where it does not
> know precisely what value x has.
>
> dosomething( X x )
> {
> Y o6 = (Y)x;`
> }
This is taken from my professor's sample midterm. According to him,
the answer is right. I emailed him back saying the compiler generated
an error. He replied back "stop by my office hours sometimes this week
so we can discuss it". That's really not feasible since the midterm is
tomorrow morning.
Chad
[toc] | [prev] | [next] | [standalone]
| From | Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> |
|---|---|
| Date | 2011-10-23 19:00 -0700 |
| Message-ID | <sPCdnVV3XNY6WznTnZ2dnUVZ_tudnZ2d@posted.palinacquisition> |
| In reply to | #9128 |
On 10/23/11 6:50 PM, Roedy Green wrote: > On Sun, 23 Oct 2011 18:22:38 -0700 (PDT), Chad<cdalten@gmail.com> > wrote, quoted or indirectly quoted someone who said : > >> X x = new X(); >> Y y = new Y(); >> Z z = new Z(); >> >> Y o6 = (Y) x; //<--error > > x references an X object not a Y. Perhaps the compiler is smart enough > to notice that. Perhaps it knows that cast could never succeed. He's getting a runtime error, not a compile-time error.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-23 23:22 -0700 |
| Message-ID | <15065329.1019.1319437339685.JavaMail.geo-discussion-forums@yqgd7> |
| In reply to | #9130 |
Peter Duniho wrote: > He's getting a runtime error, not a compile-time error. "Some casts can be proven incorrect at compile time; such casts result in a compile-time error." JLS 5.5 I'd have to bestir myself to confirm this, but I think the OP's situation is in this category. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-23 23:27 -0700 |
| Message-ID | <33420578.196.1319437629339.JavaMail.geo-discussion-forums@yqnv12> |
| In reply to | #9140 |
Lew wrote: > Peter Duniho wrote: >> He's getting a runtime error, not a compile-time error. > > "Some casts can be proven incorrect at compile time; such casts result in a compile-time error." > JLS 5.5 > > I'd have to bestir myself to confirm this, but I think the OP's situation is in this category. OK, I am being very confused. The OP posted which it was. Please return to your useful thread already in progress. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> |
|---|---|
| Date | 2011-10-23 19:03 -0700 |
| Message-ID | <mY2dnWeeMpD2WjnTnZ2dnUVZ_g2dnZ2d@posted.palinacquisition> |
| In reply to | #9126 |
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
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-10-23 23:16 -0400 |
| Message-ID | <j82las$i9t$1@dont-email.me> |
| In reply to | #9126 |
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
[toc] | [prev] | [next] | [standalone]
| From | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| Date | 2011-10-24 07:19 +0300 |
| Message-ID | <785fn8-05j.ln1@hurricane.fredriksson.dy.fi> |
| In reply to | #9133 |
24.10.2011 6:16, Eric Sosman kirjoitti: > > Because all Toyotas are Automobiles, but not all Automobiles > are Toyotas. > Not true. Toyota has made also sewing machines and forklifts, dunno other products. :p -- Good day for overcoming obstacles. Try a steeplechase.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2011-11-06 16:03 -0500 |
| Message-ID | <4eb6f63c$0$290$14726298@news.sunsite.dk> |
| In reply to | #9135 |
On 10/24/2011 12:19 AM, Donkey Hottie wrote: > 24.10.2011 6:16, Eric Sosman kirjoitti: >> Because all Toyotas are Automobiles, but not all Automobiles >> are Toyotas. > > Not true. Toyota has made also sewing machines and forklifts, dunno > other products. :p http://en.wikipedia.org/wiki/Toyota#Non-automotive_activities lists a few other things, but I don't think it is so relevant for Eric's point. Arne
[toc] | [prev] | [next] | [standalone]
| From | Joshua Cranmer <Pidgeot18@verizon.invalid> |
|---|---|
| Date | 2011-10-24 00:08 -0500 |
| Message-ID | <j82rrq$e51$1@dont-email.me> |
| In reply to | #9126 |
On 10/23/2011 8:22 PM, Chad wrote: > I thought I could cast X to Y since they inherited. Why doesn't it > work in this case? The cast is legal Java code and compiles correctly; the fact you got a runtime exception confirms this. However, you instantiated a runtime object of type X, which is not a runtime object of type Y. For a clearer analogue, consider the syntactically similar code: Object o = "ASDF"; Integer i = (Integer)o; Do you believe this should work at runtime? -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-23 23:17 -0700 |
| Message-ID | <6963560.1010.1319437079999.JavaMail.geo-discussion-forums@yqgd7> |
| In reply to | #9126 |
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
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-23 23:19 -0700 |
| Message-ID | <224194.2272.1319437162135.JavaMail.geo-discussion-forums@yqgn17> |
| In reply to | #9138 |
Lew wrote: > And by the way, you don't get an error for that, you get an exception for that. Oops, I'm wrong. That one is an error. -- Lew
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web