3 ms·
It comes down to the fact that interfaces aren't classes. To whit: let's say you want to have an interface for associative arrays. Let's call it Map. Now let's
by numbsafari 14y ago
It comes down to the fact that interfaces aren't classes.
To whit: let's say you want to have an interface for associative arrays. Let's call it Map. Now let's say you want to provide a static method to create an instance of the default Map implementation (call it, say, HashMap). So you'd think about doing something like this:
interface Map {
...
static defaultImpl() {
....
}
}
Except, interfaces can't have implementation in them. That would imply multiple inheritance (which languages like Dart and Java don't support).
Okay, so one workaround: use an abstract class:
abstract class Map {
...
static defaultImpl() {
....
}
}
The only problem is that if you define Map as an abstract class instead of an interface, that means that all implementations of Map must extend Map. Given single inheritance, this means anything providing a Map-style interface can and must only extend from the base Map class.
So... Factory classes are there because of how Java treats interfaces and single inheritance.