[3416] in java-interest
Re: New Java mailinglist/usergroup for SouthernCA
daemon@ATHENA.MIT.EDU (Michael Lorton)
Fri Nov 10 19:12:29 1995
Date: Fri, 10 Nov 1995 12:12:44 -0800
From: Michael Lorton <mlorton@eshop.com>
To: michael@w3media.com
Cc: java-interest@java.sun.com
In-Reply-To: <199511100749.XAA11033@vv.val.net> (michael@w3media.com)
> Individuals (especially the ones able to explain *casting classes* to me ;)
> are welcome to contact me at:
You cannot cast a class. You cannot even cast an object. You can
only cast a reference to an object.
Consider the following example:
class A {
public int a;
} ;
class B extends A {
public float f;
} ;
class C extends A {
public int f;
} ;
public class X {
public void setThree(A aa)
aa.f = 3; // ***
}
}
If the starred line were legal, it would be completely ambiguous -- is
aa a reference to a B object? Is it a C object? Maybe it's just a
typo.
The correct line is
((B)aa).f = 3;
If aa is *not* a reference to a B object, an exception will be thrown
at runtime.
You needn't (explicitly) cast a object's reference to a base class --
since there is no MI (yeaaa), there is no chance of ambiguity.
M.
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com