Sobes.tech
Junior

What is an interface in the context of programming?

sobes.tech AI

Answer from AI

In the context of programming, an interface defines a set of methods, properties, or events that a class must implement. It describes what a class should do, but not how it does it. It is a contract that obligates implementing classes to provide certain functionality. Interfaces provide abstraction and polymorphism, allowing work with objects of different classes through a common interface.

Example in Dart:

// Defining an interface (using an abstract class in Dart)
abstract class Printable {
  void printContent();
}

// Class implementing the interface
class Document implements Printable {
  @override
  void printContent() {
    // Implementation of printContent for a document
    print('Printing document content...');
  }
}

// Another class implementing the same interface
class Report implements Printable {
  @override
  void printContent() {
    // Implementation of printContent for a report
    print('Printing report content...');
  }
}

void main() {
  // Working with objects through the common Printable interface
  Printable doc = Document();
  Printable report = Report();

  doc.printContent();
  report.printContent();
}
What is an interface in the context of programming… - sobes.tech