Sobes.tech
Junior — Middle

Explain what abstraction is and how it is applied in programming.

sobes.tech AI

Answer from AI

Abstraction is a programming principle that allows hiding complex implementation details and showing only the necessary interface for interacting with an object or system.

In programming, abstraction helps focus on what an object does rather than how it does it. This simplifies understanding, maintenance, and extension of the code.

In Flutter (and Dart), abstraction is applied through:

  • Abstract classes and methods — define an interface without implementation, which subclasses must provide.
  • Interfaces — in Dart, any class can serve as an interface that other classes implement.

Example of an abstract class in Dart:

abstract class Animal {
  void makeSound(); // abstract method
}

class Dog extends Animal {
  @override
  void makeSound() {
    print('Woof');
  }
}

void main() {
  Animal dog = Dog();
  dog.makeSound(); // Outputs: Woof
}

Here, Animal defines an abstract interface, and Dog implements specific behavior. The user interacts with Animal without knowing the details of Dog's implementation.