Junior — Middle
What is the reason for introducing the abstract keyword in Dart 2.12?
sobes.tech AI
Answer from AI
The keyword abstract in Dart 2.12 was introduced to explicitly denote abstract classes. Previously, abstract classes were defined implicitly, for example, if a class contained abstract methods (without implementation). The introduction of abstract allows:
- Explicitly marking a class as abstract, which improves code readability.
- Preventing direct instantiation of such classes.
- Enhancing support for static analysis and development tools.
Example of usage:
abstract class Animal {
void makeSound(); // abstract method
}
class Dog extends Animal {
@override
void makeSound() {
print('Bark');
}
}
Here, Animal is an abstract class that cannot be instantiated directly.