Having this snippet:
abstract class SuperClass {
static String id = 'id';
static String get getId {
return id;
}
}
class SubClass extends SuperClass {
static String name = 'name';
}
void main() {
print(SubClass.name);
print(SubClass.id);
}
How can i access id
property?
print(SubClass.id);
results in error:
"line 3 • The getter 'id' isn't defined for the type 'SubClass'."
Doesn't SubClass
should inherit the id
property when extended (not implemented)?
CodePudding user response:
In Dart, static methods and properties are not inherited.
This is covered in section 10.7 of the Dart language spec:
10.7 Static Methods staticMethods Static methods are functions, other than getters or setters, whose declara- tions are immediately contained within a class declaration and that are declared static. The static methods of a class C are those static methods declared by C.
Inheritance of static methods has little utility in Dart. Static methods cannot be overridden. Any required static function can be obtained from its declaring library, and there is no need to bring it into scope via inheritance. Experience shows that developers are confused by the idea of inherited methods that are not instance methods.
Of course, the entire notion of static methods is debatable, but it is retained here because so many programmers are familiar with it. Dart static methods may be seen as functions of the enclosing library.
Static method declarations may conflict with other declarations (10.10).
Other References