Can one declare a static method within an abstract class, in Dart? Can one declare a static method within an abstract class, in Dart? dart dart

Can one declare a static method within an abstract class, in Dart?


Dart doesn't inherit static methods to derived classes. So it makes no sense to create abstract static methods (without implementation).

If you want a static method in class Main you have to fully define it there and always call it like Main.name

== EDIT ==

I'm sure I read or heard some arguments from Gilad Bracha about it but can't find it now.

This behaviour is IMHO common mostly in statically typed languages (I don't know many dynamic languages). A static method is like a top level function where the class name just acts as a namespace. A static method has nothing to do with an instantiated object so inheritance is not applicable. In languages where static methods are 'inherited' this is just syntactic sugar. Dart likes to be more explicit here and to avoid confusion between instance methods and static methods (which actually are not methods but just functions because they don't act on an instance). This is not my primary domain, but hopefully may make some sense anyways ;-)


Looks like you are trying to 'override' a static method. I'm not sure what you are trying to achieve there. I'm not aware of any OO languages that support that (and not sure how they could).

A similar question in Java might help clarify Polymorphism and Static Methods

Note also that it is considered bad practice to refer to statics from an instance of the class in Java (and other OO languages). Interestingly I noticed Dart does not let you do this so is in effect removing this bad practice entirely.

So you couldn't even fool yourself into thinking it would behave polymorphically in Dart because you can't call the static from the instance.