Sobes.tech
Junior — Middle

Explain what the 'extends' keyword means in the context of object-oriented programming.

sobes.tech AI

Answer from AI

In object-oriented programming, the keyword extends is used to denote inheritance. It allows one class (subclass) to inherit properties and methods from another class (superclass), extending or overriding them.

In the context of Flutter (Dart), extends is used, for example, when creating custom widgets or classes that inherit functionality from base classes.

Example:

class Animal {
  void speak() {
    print('Animal sound');
  }
}

class Dog extends Animal {
  @override
  void speak() {
    print('Bark');
  }
}

void main() {
  Dog dog = Dog();
  dog.speak(); // Outputs: Bark
}

Thus, extends allows creating class hierarchies and reusing code.