5.2
Assignment Conversion
CONVERSIONS AND PROMOTIONS
If
S
is an interface type:
N
If
T
is a class type, then
T
must be
Object
, or a compile time error occurs.
N
If
T
is an interface type, then
T
must be either the same interface as
S
or a
superinterface of
S
, or a compile time error occurs.
N
If
T
is an array type, then a compile time error occurs.
If
S
is an array type
SC[]
, that is, an array of components of type
SC
:
N
If
T
is a class type, then
T
must be
Object
, or a compile time error occurs.
N
If
T
is an interface type, then a compile time error occurs unless
T
is the
interface type
Cloneable
, the only interface implemented by arrays.
N
If
T
is an array type
TC[]
, that is, an array of components of type
TC
,
then a
compile time error occurs unless one of the following is true:
O
TC
and
SC
are the same primitive type.
O
TC
and
SC
are both reference types and type
SC
is assignable to
TC
,
as
determined by a recursive application of these compile time rules for
assignability.
See 8 for the detailed specifications of classes, 9 for interfaces, and 10 for
arrays.
The following test program illustrates assignment conversions on reference
values, but fails to compile because it violates the preceding rules, as described in
its comments. This example should be compared to the preceding one.
public class Point { int x, y; }
public interface Colorable { void setColor(int color); }
public class ColoredPoint extends Point implements Colorable
{
int color;
public void setColor(int color) { this.color = color; }
}
class Test {
public static void main(String[] args) {
Point p = new Point();
ColoredPoint cp = new ColoredPoint();
//
Okay because
ColoredPoint
is a subclass of
Point
:
p = cp;
//
Okay because
ColoredPoint implements Colorable
:
Colorable c = cp;
64
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved