In Java one can declare a method within a class to prevent subclasses from overriding it.
For example:
class Foo {
final void bar() {
// some code here.
}
}
Is there a similar construction in Dart?
In Java one can declare a method within a class to prevent subclasses from overriding it.
For example:
class Foo {
final void bar() {
// some code here.
}
}
Is there a similar construction in Dart?
package:meta provides a @nonVirtual annotation to disallow overriding methods and a @sealed annotation to disallow derived classes entirely.
Note that these annotations just provides hints to dartanalyzer. They won't actually prevent anything from violating the annotations, and they instead will cause warnings to be printed when analysis is performed.
Yes, code for Irn & Jamesdlin answer:
import 'package:meta/meta.dart';
class Foo {
@nonVirtual
void bar() {
print("bar from Foo!");
}
}
class SubFoo extends Foo {
@override
void bar() {
print("bar from SubFoo");
}
}
And you get the warning from Analyzer like this:
The member 'bar' is declared non-virtual in 'Foo' and can't be overridden in subclasses.dart(invalid_override_of_non_virtual_member)