Junior — Middle
Can classes with final implementation be declared, and what is their feature?
sobes.tech AI
Answer from AI
In Dart (the language used in Flutter), classes are not "final" by default in the sense that they cannot be inherited. However, starting from Dart 2.13, it became possible to declare classes with the final keyword, which prohibits inheritance.
The feature of such classes is that they have a final implementation — they cannot be extended or overridden. This is useful for creating immutable APIs or when you need to guarantee that the class's behavior will not be changed through inheritance.
Example of declaring a final class:
final class MyFinalClass {
void doSomething() {
print('Performing an action');
}
}
// Attempting to inherit will cause an error
// class ChildClass extends MyFinalClass {} // Error
Thus, classes with a final implementation provide stricter control over the class hierarchy and help prevent unintentional extension.