This article covers a bit more details. And this question answers it more broadly.
But the summary is, I agree with you, and that's why you should use abstract interface
instead of just interface
. You'd only use interface
over abstract interface
if you wanted to be able to instantiate it.
abstract interface
What is it: More like a traditional interface. Can only be implemented (not extended). But you can define functions without bodies.
Why you should care: You can define just the “shape” without defining any functionality. There’s nothing hidden in the parent class.
// -- File a.dart
abstract interface class AbstractInterfaceClass {
String name = 'Dave'; // Allowed
void body() { print('body'); } // Allowed
// This is a more traditional implementation
int get myField; // Allowed
void noBody(); // Allowed
}
// -- File b.dart
// Not allowed
class ExtensionClass extends AbstractInterfaceClass{}
// Allowed
class ConcreteClass implements AbstractInterfaceClass {
// Have to override everything
@override
String name = 'ConcreteName';
@override
void body() { print('body'); }
@override
int get myField => 5;
@override
void noBody() => print('concreteBody');
}
vs interface
// -- File a.dart
interface class InterfaceClass {
String name = 'Dave'; // Allowed
void body() { print('body'); } // Allowed
int get myField; // Not allowed
void noBody(); // Not allowed
}
// -- File b.dart
// Not allowed
class ExtensionClass extends InterfaceClass{}
// Allowed
class ConcreteClass implements InterfaceClass{
// Have to override everything
@override
String name = 'ConcreteName';
@override
void body() { print('body'); }
}
interface
class if you want a class to be constructible, implementable, but not extendable. If you don't want it to be constructible, then use anabstract interface class
. There's a helpful table in the feature specification. – Regionalism