Skip to content

Commit 23fd8ec

Browse files
MaryaBelangeratsansoneparlough
authored
Glossary entries for "Subtype" and "Subclass" (#4958)
Fixes #4784 --------- Co-authored-by: Anthony Sansone <atsansone@users.noreply.github.com> Co-authored-by: Parker Lougheed <parlough@gmail.com>
1 parent b4f0968 commit 23fd8ec

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/content/resources/glossary.md

+57
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,60 @@ If not, the pattern _refutes_, or denies, the match.
279279
Refutable patterns appear in [_matching contexts_][].
280280

281281
[_matching contexts_]: /language/patterns#matching
282+
283+
## Subclass
284+
285+
A _subclass_ is a class that inherits the implementation of another class by using the
286+
[`extends`](/language/extend) keyword, or by [mixin application](#mixin-application).
287+
288+
```dart
289+
class A extends B {} // A is a subclass of B; B is the superclass of A.
290+
291+
class B1 extends A with M {} // B1 has the superclass `A with M`, which has the superclass A.
292+
```
293+
294+
A subclass relation also implies an associated [subtype](#subtype) relation.
295+
For example, `class A` implicitly defines an associated type `A`
296+
which instances of the class `A` inhabit.
297+
So, `class A extends B` declares not just that the class
298+
`A` is a subclass of `B`, but also establishes that the *type* `A` is a
299+
*subtype* of the type `B`.
300+
301+
Subclass relations are a subset of subtype relations.
302+
When the documentation says "`S` must be a subtype of `T`",
303+
it's fine for `S` to be a subclass of `T`.
304+
However, the converse is not true: not all subtypes are subclasses.
305+
See the [subtype](#subtype) entry for more information.
306+
307+
## Subtype
308+
309+
A _subtype_ relation is where a value of a certain type is substitutable
310+
where the value of another type, the supertype, is expected.
311+
For example, if `S` is a subtype of `T`,
312+
then you can substitute a value of type `S`
313+
where a value of type `T` is expected.
314+
315+
A subtype supports all of the operations of its supertype
316+
(and possibly some extra operations).
317+
In practice, this means you can assign the value of a subtype
318+
to any location expecting the supertype,
319+
and all of the methods of the supertype are available on the subtype.
320+
321+
This is true at least statically.
322+
A specific API might not allow the substitution at run time,
323+
depending on its operations.
324+
325+
Some subtype relations are based on the structure of the type,
326+
like with nullable types (for example, `int` is a subtype of `int?`)
327+
and function types
328+
(for example, `String Function()` is a subtype of `void Function()`).
329+
330+
Subtypes can also be introduced for classes by
331+
[implementation](/language/classes#implicit-interfaces)
332+
or [inheritance](/language/extend) (direct or indirect):
333+
334+
```dart
335+
class A implements B {} // A is a subtype of B, but NOT a subclass of B.
336+
337+
class C extends D {} // C is a subtype AND a subclass of D.
338+
```

0 commit comments

Comments
 (0)