[469] in java-interest
Re: Array Subclassing and IncompatibleTypeException
daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Fri Jun 23 12:46:42 1995
From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
To: majordom@java.Eng.Sun.COM (Patrick Doane)
Date: Fri, 23 Jun 1995 09:12:19 -0800 (PDT)
Cc: java-interest@java.Eng.Sun.COM
In-Reply-To: <v01510100ac1078fc7730@[204.213.233.119]> from "Patrick Doane" at Jun 23, 95 10:04:12 am
Hi Patrick,
> >Recently Bill Joy, Guy Steel, David Ungar and Ole Madsen have been
> >evaluating the Java language and they have pointed out that our current
> >array type system may not be the optimal choice. As it turns we only
> >allow co-variant argument types on array types and not on class types.
> >We are working on a proposal which, in the future, allows us to express
> >arrays as parameterized types without the requirement for co-variant arguments.
> >I'll let you know the result of this discussion.
>
> I'm interested in exactly what this means. I've had very little formal
> Computer Science training and the words here are confusing me. Any help
> would be appreciated in understanding this.
Here is how arrays conceptually work in the current version of Java:
class Foo { ... }
class Bar extends Foo { ... }
cass ArrayOfFoo extends ArrayOfObject {
Foo get(int i) { ... }
Foo put(int i, Foo f) { ... }
}
cass ArrayOfBar extends ArrayOfFoo {
Bar get(int i) { ... }
Bar put(int i, Bar f) { ... }
}
Unfortunately, this doesn't actually work, because the
get and put methods don't override each other because
they have different argument types. Imagine they did
override each other, then you could get into trouble:
ArrayOfBar b = ...;
ArrayOfFoo f = b;
f.put(0, new Foo());
Note that this would stick a Foo in an array of Bar. Bad!
In order to make this work somehowe we would have to allow
the methods override each other and at runtime we would
have to do runtime checks. This is called covarient subtyping.
We currently treat arrays different than other classes. We do
the type checks on assignment, and thus, we under the covers
implement covariant subtype on arrays, but not on other classes.
There is a proposal that may avoid some of this uglyness using
parameterized types to define arrays. More about that later.
Have fun,
Arthur van Hoff (avh@eng.sun.com)
http://java.sun.com/people/avh/
Sun Microsystems Inc, M/S UPAL02-301,
100 Hamilton Avenue, Palo Alto CA 94301, USA
Tel: +1 415 473 7242, Fax: +1 415 473 7104
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com