简体   繁体   中英

What Is The Purpose of @override used in Flutter?

In the flutter documentation the keyword @override is used a lot in classes. I tried to search for the meaning but still can't understand. What is the purpose of this @override Keyword.

Note: I come from a JavaScript background.

@override just points out that the function is also defined in an ancestor class, but is being redefined to do something else in the current class. It's also used to annotate the implementation of an abstract method. It is optional to use but recommended as it improves readability.

The annotation @override marks an instance member as overriding a superclass member with the same name.

https://api.dartlang.org/stable/1.24.3/dart-core/override-constant.html

JavaScript also supports @override as an annotation, but it has to be in a comment. http://usejsdoc.org/tags-override.html .

The annotation @override marks an instance member as overriding a superclass member with the same name.

The annotation applies to instance methods, getters and setters, and to instance fields, where it means that the implicit getter and setter of the field is marked as overriding, but the field itself is not.

The intent of the @override notation is to catch situations where a superclass renames a member, and an independent subclass which used to override the member, could silently continue working using the superclass implementation.

The editor, or a similar tool aimed at the programmer, may report if no declaration of an annotated member is inherited by the class from either a superclass or an interface.

Use the @override annotation judiciously and only for methods where the superclass is not under the programmer's control, the superclass is in a different library or package, and it is not considered stable. In any case, the use of @override is optional.

For example, the annotation is intentionally not used in the Dart platform libraries, since they only depend on themselves.

Taken from https://docs.flutter.io/flutter/dart-core/override-constant.html

What, exactly, do we mean by these terms? Inheritance is the ability of a class to inherit properties and methods from a superclass (and from the superclass's superclass, and so on). Polymorphism is exemplified in Dart by the @override metatag. With it, a subclass's implementation of an inherited behavior can be specialized to be appropriate to its more specific subtype. When a class has properties that are themselves instances of other classes, it's using composition to add to its abilities.

Inheritance and polymorphism in Flutter

The Flutter UI framework, all written in open-source Dart, is full of examples of inheritance and polymorphism. In fact, many of the standard patterns in building Flutter apps rely on these concepts:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

This is the simplest example of a custom widget in Flutter. Note that it uses the extends keyword to indicate that the class should inherit properties and methods from StatelessWidget, which itself inherits from the Widget class. This is important, because every Flutter widget has a build() method available that returns an instance of Widget. The @override metatag helps you identify inherited methods or variables that are being overridden (replaced) in your subclass.

For more info, see: https://dart.academy/inheritance-polymorphism-and-composition-in-dart-and-flutter/

@override modifier keyword is very similar to the keyword in C# programming language in that it indicates that the member of a class (ie child class) is overriding and redefining the corresponding member from a base class (ie parent class). So in flutter...

@override
Widget build(BuildContext context) {...}

... the build class is redefined in a custom build widget.

According to docs
The annotation @override marks an instance member as overriding a superclass member with the same name.

Example

class A {
  void foo() {
    print("Class A");
  }
}

class B extends A {}

class C extends A {
  @override foo() {
    print("Class C");
  }
}

void main() {
  A a = A();
  B b = B();
  C c = C();
  a.foo();
  b.foo();
  c.foo();
}

The Output is:

Class A
Class A
Class C

What happening is B extends A and hence inherits all the methods of A . Hence when b.foo() is called it calls method from its parent class.

But in C it has foo with override keyword which means it will override foo declaration in its parent class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM