thumbnail

Interfaces In Dart

Published on 29 March 2020
Last Updated on 29 March 2020

Whenever a class is defined, it implicitly defines an interface containing all the instances of all instance members and all interfaces extended by the class itself.

Interfaces can be useful for defining classes that can support APIs of other class without actually extending from a different class.

In Dart, interfaces can be used as a blueprint of a class. If this blueprint is implemented by a different subclass then that subclass must adhere to that blueprint.

Declaring Interfaces

In Dart, there is no direct way to declare an interface, instead classes themselves are implicitly interfaces, instance members of the class are implicitly converted to interfaces.

Implements keyword

Implements keyword is used by a class when implementing an interface from a different class. Once implements keyword is used, the class that makes use of the implements keyword must satisfy all implicitly defined interfaces.

If an interface is implemented but not satisfied then Dart analysis will display an error message.

Multiple classes can be implemented as interfaces using the implements keyword.

Program to demonstrate use of Interfaces in Dart

Program given below makes use of interface Quadrilateral for class Square.

// A Quadrilateral class
// this class is an implicit interface
class Quadrilateral {
  // private variables begin with _
  // private variables are only visible in the library itself
  // all variables given below are available in the interface
  double _sideA;
  double _sideB;
  double _sideC;
  double _sideD;

  // constructors are not available in the interface
  Quadrilateral(
    this._sideA,
    this._sideB,
    this._sideC,
    this._sideD,
  );

  // available in the interface
  double perimeter() {
    return _sideA + _sideB + _sideC + _sideD;
  }
}

class Square implements Quadrilateral {
  // implementation mandatory
  double _sideA;
  double _sideB;
  double _sideC;
  double _sideD;

  double side;

  // implementation is not mandatory
  Square(
    this.side,
  ) {
    _sideA = side;
    _sideB = side;
    _sideC = side;
    _sideD = side;
  }

  // implementation mandatory
  double perimeter() {
    return _sideA + _sideB + _sideC + _sideD;
  }
}

main(List<String> args) {
  var q = new Quadrilateral(1, 2, 3, 4);
  var p1 = q.perimeter();

  var square = new Square(2);
  var p2 = square.perimeter();

  print('perimeter of quadrilateral is: ${p1}');
  print('perimeter of square is: ${p2}');
}

Above program produces following output:

perimeter of quadrilateral is: 10.0
perimeter of square is: 8.0

Program to demonstrate use of multiple interfaces

Program given below demonstrates how multiple interfaces can be used in Dart:

// program to demonstrate use of multiple doubleerfaces in Dart

class Add {
  double a, b;

  double add() {
    return a + b;
  }
}

class Subtract {
  double a, b;

  double sub() {
    return a - b;
  }
}

class Multiply {
  double a, b;

  double multiply() {
    return a * b;
  }
}

class Divide {
  double a, b;

  double divide() {
    return a / b;
  }
}

class Arithmetic implements Add, Subtract, Multiply, Divide {
  double a, b;

  Arithmetic(this.a, this.b);

  @override
  double add() {
    return a + b;
  }

  @override
  double sub() {
    return a - b;
  }

  @override
  double multiply() {
    return a * b;
  }

  @override
  double divide() {
    return a / b;
  }
}

main(List<String> args) {
  var a = new Arithmetic(4, 9);
  print('addition is: ${a.add()}');
  print('subtraction is: ${a.sub()}');
  print('multiplication is: ${a.multiply()}');
  print('division is: ${a.divide()}');
}

Above program produces following output:

addition is: 13.0
subtraction is: -5.0
multiplication is: 36.0
division is: 0.4444444444444444

As you can see in above program, class Arithmetic implements multiple interfaces, this interfaces is a mechanism by which we can achieve multiple inheritance in Dart.