Reputation: 1055
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.
Upvotes: 47
Views: 47020
Reputation: 3987
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.
Upvotes: 9
Reputation: 11
@override modifier keyword is very similar to the keyword in C# programming language in that it indicates that the member of a class (i.e. child class) is overriding and redefining the corresponding member from a base class (i.e. parent class). So in flutter...
@override
Widget build(BuildContext context) {...}
... the build class is redefined in a custom build widget.
Upvotes: 1
Reputation: 5533
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
Upvotes: 3
Reputation: 9274
@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.
Upvotes: 58