Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't visit static variable of parent class in Dart?

Tags:

dart

Dart code:

main() {
    print(PPP.name);
    print(CCC.name);
}

class PPP {
    static String name = "PPP";
}

class CCC extends PPP {
}

It prints:

PPP
Unhandled exception:
No static getter 'name' declared in class 'CCC'.

NoSuchMethodError : method not found: 'name'
Receiver: Type: class 'CCC'
Arguments: [...]

So it's not available to visit static variables of parent class in Dart?

like image 496
Freewind Avatar asked Feb 16 '23 22:02

Freewind


1 Answers

From Dart Programming Language Specification:

The static members of a class are its static methods, getters, setters and static variables.

  • Superclass static members are not in scope in subclasses, and do not conflict with subclass members.
  • Static members are never inherited.
  • Static members never override anything.

So if you declare some static members in superclass then these members are not inherited in subclasses.

They remain in that class where they declared and do not conflicts with other declarations static members in the subclasses.


Q: Can't visit static variable of parent class in Dart?

A: Static variable of parent class cannot be accessed (as its own) in child class because it does not exists (not inherited) in child class.

like image 113
mezoni Avatar answered Apr 10 '23 12:04

mezoni