Subtyping and the Type Hierarchy
[comp1110-8D]
Subtyping and the Type Hierarchy
[comp1110-8D]You can think of types as essentially mathematical sets of values.
For example, the type int in Java is the set of all 32-bit signed integer values (i.e. from \(-2^{31}\) to \(2^{31}-1\) (\(2^{31}\) is a bit over 2 billion)).
Similarly, the type String is the set of all potential String objects, plus null, because in Java, null is a value of every reference type.
The set of values of a subtype, then, is a subset of the values of any supertypes.
Every reference type is (at least transitively) a subtype of the special class type Object, even the interface types (otherwise, interface types can generally not be subtypes of class types, because interfaces cannot extend classes).
From Java's perspective, subtyping relationships are created through (potentially implicit) extends and implements declarations and transitivity.
Java checks them for well-formedness in the sense that it checks that any overridding methods are compatible with the overridden ones in their superclass, and any unimplemented methods inherited from interfaces are actually implemented in classes declaring to implement those interfaces.
Where Java considers a subtyping relationship to exist (based on the declarations), it allows using a value of the subtype as one of the supertype.