The interface is one of the object-oriented concepts in Dart language.

The interface is a blueprint of class objects

like java language, the interface keyword is removed from the dart.

How to declare an interface in Dart

There are two ways we can declare an interface in Dart.

  • Implicit interface using normal class
  • Explicit interface using abstract class

Error: The non-abstract class ‘Lion’ is missing implementations for these members:

  • Implicit interface using normal class Every class declares in dart is an implicit interface, Which means we can provide an implementation class using implement for every class.

Syntax:

class Interface1{
// Allows methods with body
// does not allow method declaration
}



class ImplementationClass implements Interface1{
  // provide the implementation for all methods in interface
}

Interface1 and ImplementationClass are valid identifier class names in dart.

Following is an example for implicit interface

In the below example,

  • class Animal is declared and dart treat it as an interface
  • provide implementation class Lion for Animal interface and override the behavior
  • Create an object using Lion class
void main() {
  // Creating Object of the implementation class
  Lion lion = new Lion();
  lion.displayType();
}

//  animal interface
class Animal {
  void displayType() {
    print("Animal");
  }
}

// Class Lion implementing Animal
class Lion implements Animal {
  void displayType() {
    print("Lion");
  }
}

Disadvantages with implicit interface

  • Every implicit interface allows only non-abstract methods

Let’s add the Animal interface with function declaration without body and it throws an error Error: The non-abstract class 'Animal' is missing implementations for these members:

//  animal interface
class Animal {
  void displayType() {
    print("Animal");
  }
    void displayType1();
}
  • Abstract interface:

In this abstract keyword is used to define the interface You can also declare an interface explicit using the abstract keyword.

the abstract interface allows abstract methods Here is the syntax.

abstract class Interface1{
// Allows methods with body
// Allows method declaration( Abstract method)
}

class ImplementationClass implements Interface1{
  // provide the implementation for all methods in interface
}

Interface1 and ImplementationClass are valid identifier class names in dart.

Here is an abstract explicit interface example

  • abstract interface contains abstract and non-abstract methods
  • implementation class must provide or override the methods
void main() {
  Lion lion = new Lion();
  lion.displayType();
}

//  animal interface
abstract class Animal {
  void displayType() {
    print("Animal");
  }

  void displayType1();
}

// Class Lion implementing Animal
class Lion implements Animal {
  void displayType() {
    print("Lion");
  }

  void displayType1() {
    print("Lion displayType1");
  }
}

This is a valid interface with non-abstract methods

//  animal interface
abstract class Animal {
  void displayType() {
    print("Animal");
  }
    void displayType1();
}

if interface(implicit or explicit) is implemented in another class, It must provide the implementation for all the methods, Otherwise, it gives an error Error: The non-abstract class ‘Lion’ is missing implementations for these members:

The below code throws an error if methods non implemented in a class(Lion)

//  animal interface
abstract class Animal {
  void displayType() {
    print("Animal");
  }
    void displayType1(){
      
    }
}

// Class Lion implementing Animal
class Lion implements Animal {
  void displayType() {
    print("Lion");
  }
 
}

Dart multiple inheritance using the interface

Dart language supports multiple inheritances by implementing multiple interfaces or classes.

Since the class is an implicit interface. It can support multiple interfaces with comma-separated in a class Syntax

class Interface1{
// Allows methods with body
// does not allow method declaration
}

abstract class Interface2{
// Allows methods with or without body
}

class ImpleentationClass implements Interface1,Interface2{
  // provide the implementation for all methods in interface
}
void main() {
  Rectangle rec = new Rectangle();
  rec.displayType();
  rec.calculateArea();
}

//  Shape interface
abstract class Shape {
  void displayType() {
    print("Shape");
  }
}
//  Area interface

abstract class Area {
  void calculateArea() {
    print("Area");
  }
}

// Class Rectangle implementing Shape and Area
class Rectangle implements Shape, Area {
  void displayType() {
    print("Rectangle");
  }

  void calculateArea() {
    print("Rectangle area");
  }
}

Why use a dart interface?

There are benefits to using interfaces in dart

  • It used to provide the abstraction
  • It supports multiple inheritances
  • With interfaces, Louse coupling between implementations