Sobes.tech
Junior — Middle

Mis on põhjuseks, miks Dart 2.12 versioonis lisati märksõna abstract?

sobes.tech AI

Vastus AI-lt

The abstract keyword 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.
  • Prohibiting direct instantiation of such classes.
  • Improving 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.