Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34087 > unrolled thread

Re: IsSupertypeOf

Started byDaniel Pitts <usenet@coloraura.com>
First post2016-04-07 22:19 -0700
Last post2016-04-07 22:19 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: IsSupertypeOf Daniel Pitts <usenet@coloraura.com> - 2016-04-07 22:19 -0700

#34087 — Re: IsSupertypeOf

FromDaniel Pitts <usenet@coloraura.com>
Date2016-04-07 22:19 -0700
SubjectRe: IsSupertypeOf
Message-ID<ne7er8$ar1$1@dont-email.me>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web