practicebug
practicebug

Reputation: 31

In Dart, it is not possible to override a static method. but i can't understand output of this code

class Student {
  static void getDetails() {
    print('Get details method of Student class');
  }
}

class DartStudent extends Student {
  static void getDetails() {
    print('Get details method of DartStudent class');
  }
}

void main() {
  DartStudent.getDetails();
}
Output : Get details method of DartStudent class

Expected : Error. static method cannot be overriden.. or something wrong..


what's wrong with me?
getDetails() in DartStudent class is not overriding parent class's method?

Upvotes: 2

Views: 2796

Answers (1)

Victor
Victor

Reputation: 14612

You can't override static methods.

The two static methods you declared there are in fact two different static methods, not the same, overriden one.

Answer for a different question, but related:

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

Also check out Why shouldn't static methods be able to be overrideable?. It provides a thorough explanation of why static methods should not be overrideable in general.

Upvotes: 5

Related Questions