@override of Dart code @override of Dart code dart dart

@override of Dart code


From @override doc :

An annotation used to mark an instance member (method, field, getter or setter) as overriding an inherited class member. Tools can use this annotation to provide a warning if there is no overridden member.

So, it depends on the tool you use.

In the current Dart Editor(r24275), there's no warning for the following code but it should (it looks like a bug).

import 'package:meta/meta.dart';class A {  m1() {}}class B extends A {  @override m1() {} // no warning because A has a m1()  @override m2() {} // tools should display a warning because A has no m2()}


The @override annotation is an example of metadata. You can use Mirrors to check for these in code. Here is a simple example that checks if the m1() method in the child class has the @override annotation:

import 'package:meta/meta.dart';import 'dart:mirrors';class A {  m1() {}}class B extends A {  @override m1() {}}void main() {  ClassMirror classMirror = reflectClass(B);  MethodMirror methodMirror = classMirror.methods[const Symbol('m1')];  InstanceMirror instanceMirror = methodMirror.metadata.first;  print(instanceMirror.reflectee);  // Instance of '_Override@0x2fa0dc31'}


it's 2021 . the override it's optionalUse 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. from dart api https://api.dart.dev/stable/2.10.5/dart-core/override-constant.html

example

 Class A { void say (){ print ('Say something 1') ; }       }Class B extends A {@override void adds() { // when i don't type the same name of super class function show  an                 // warning not an error say 'The method doesn't override an inherited              //  method.' because it's  not same name but when type the same name must be              // overridingprint ('Say something 2 ')           }

Update : the main use of @override is when try to reach abstract method inside abstract class in sub class that inherited to the abstract super class . must use @override to access the abstract method .