In Dart, we say that a subclass extends its superclass with new functionality. As you will see in Chapter 9, Project: Extracting Libraries, most of a collection’s functionality can be factored out into a HipsterCollection superclass. The Comics subclass then needs to extend HipsterCollection with only a few methods.
classes/subclasses.dart | |
| class Comics extends HipsterCollection { |
| String get url => '/comics'; |
| HipsterModel modelMaker(attrs) => new ComicBook(attrs); |
| } |
The extends keyword has the obvious benefit of reading nicely, which increases a codebase’s overall maintainability.
In the previous code example, both url and modelMaker are abstract methods in the base class. Abstract methods are just methods without bodies, which should exist only inside explicitly declared abstract classes.
| abstract class HipsterCollection { |
| String get url; |
| HipsterModel modelMaker(attrs); |
| } |
This indicates that HipsterCollection is an abstract class (that it will not work without a subclass) and is one that ideally overrides these methods. If a subclass does not implement these methods, the code will not throw a compile-time error. However, a not-implemented exception is sure to follow.
3.145.61.170