Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34087
| From | Daniel Pitts <usenet@coloraura.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: IsSupertypeOf |
| Date | 2016-04-07 22:19 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <ne7er8$ar1$1@dont-email.me> (permalink) |
| References | <IsSubtypeOf-20160407185025@ram.dialup.fu-berlin.de> |
On 4/7/16 11:05 AM, Stefan Ram wrote:
> I usually explain supertypes with the Java SE API documentation.
>
> But maybe it could also help to show programs to illustrate
> this notion.
>
> For example,
>
> void f( java.lang.String s ){ java.lang.CharSequence c = s; }
>
> compiles because »java.lang.CharSequence« is a supertype of
> »java.lang.String«. So, can one say that
>
> void f( S s ){ T t = s; }
>
> compiling is equivalent to T being a supertype of S? (Assuming
> that autoboxing and autounboxing were disabled.)
I think you're conflating "assignability" with "subtypes".
if that compiles, Objects of types S are assignable to containers of type T.
void f( int s ) { double t = s; }
This isn't saying that 'double' is a subclass of 'int'. Indeed, it
would fail Liskov Substitution Principle pretty badly.
>
> There can be no instances of »java.lang.Math«, but the following
> code still compiles:
>
> void f( java.lang.Math m ){ java.lang.Object o = m; }
>
> (it can even be executed: »f( null )«.)
>
> . BTW: One can't use
>
> ( java.lang.String )null instanceof java.lang.CharSequence
>
> , that is,
>
> ( S )null instanceof T
>
> , since this indeed prints »false« because there is no
> real object behind the left operand.
>
> So, I might have found a »compile-time« IsSupertypeOf,
> which might fail due to autoboxing, but not run-time
> IsSupertypeOf. Maybe someone sees some more errors or
> possibilities for improvements?
>
I recall reading about type theory in which "null" is the "universal
subtype" and "Object" would represent the "universal super type" in Java.
Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread
Re: IsSupertypeOf Daniel Pitts <usenet@coloraura.com> - 2016-04-07 22:19 -0700
csiph-web