> For the complete documentation index, see [llms.txt](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-1/interface.md).

# Interface

### Syntax & Properties of  interface&#x20;

{% embed url="<https://youtu.be/QH-f9W0m-zE>" %}

{% tabs %}
{% tab title="Interfaces with abstract Classes " %}

```dart
void main() {
  // Interfaces in dart
}

abstract class A {
  String a;

  void testA1();

  void testA2() {
    print("A2");
  }
}

abstract class B {
  void testB1();

  void testB2() {
    print("B2");
  }
}

class C implements A, B {
  @override
  void testB1() {}

  @override
  void testB2() {}

  @override
  String a;

  @override
  void testA1() {}

  @override
  void testA2() {}
}


```

{% endtab %}

{% tab title="Interfaces with normal Classes " %}

```dart
void main() {
  // Interfaces in dart
}

 class A {
  String a;



  void testA2() {
    print("A2");
  }
}

 class B {
  

  void testB2() {
    print("B2");
  }
}

class C implements A, B {

  @override
  void testB2() {}

  @override
  String a;

  @override
  void testA2() {}
}


```

{% endtab %}
{% endtabs %}

{% hint style="success" %}
Dart implicitly defines a class as an interface, hence called as [**implicit interface**](https://dart.dev/guides/language/language-tour#implicit-interfaces) **. As there is no  interface keyword , implicitly all classes are interfaces  .**\
***Note: This might change in the future***&#x20;
{% endhint %}

{% hint style="success" %}
You can implement multiple interfaces  at once&#x20;
{% endhint %}

{% hint style="warning" %}
Whenever you implement a interface you must override everything inside it&#x20;
{% endhint %}

### Interface : Why we use them -1 ?

{% embed url="<https://youtu.be/Si8oFM8e8M0>" %}

```dart
import 'dart:math';

void main() {
  // Interface is generally used with abstract Class
  // Though dart gives us the flexibility to use it with 
  // normal class , most of the time it will be used with abstract class 

  
  double area(Shape shape){

  
    return shape.calarea();
    
  }
  
  print(area(Square(10)));
  
}

abstract class Shape {
 
  void draw();

  double calarea();
}

class Circle implements Shape{
  int radius;

  @override
  void draw() {
    print("Drawing a Circle");
  }

  @override
  double calarea() {
    return pi*pow(this.radius,2);
  }

  Circle(this.radius);
}

class Square implements Shape {
  double length;

  @override
  void draw() {
    print("Drawing a Square");
  }

  @override
  double calarea() {
    return pow(this.length, 2);
  }

  Square(this.length);


```

![](/files/-MEIqCT33Z9LE8fATN5p)

### Interface : Why we use them -2 ?

{% embed url="<https://www.youtube.com/watch?v=g18VP1HTpqU>" %}

{% tabs %}
{% tab title="PaymentInterface" %}

```dart
void main(){
  
  
  var purchase1 = Purchase();
  
  purchase1.pay(Bkash());
  
}



abstract class PaymentInterface{
  
  void payment();
  
  
}

class Bkash implements PaymentInterface{
  
  @override
  void payment(){
    
    print("Bkash");
    // Logic for payment using Bkash 
  }
  
}


class Rocket implements PaymentInterface {
  
  @override
  void payment(){
    
    print("Rocket");
    // Logic for payment using Rocket
  }
  
}

class Visa implements PaymentInterface{
  
  @override
  void payment(){
    
    print("Visa");
    // Logic for payment using Visa
  }
  
}


class Cash implements PaymentInterface{
  
  @override
  void payment(){
    
    print("Cash");
    // Logic for payment using Cash 
  }
  
}

//--------------------


class Purchase{
  
  void pay(PaymentInterface paymenttype){
    
    
    
    paymenttype.payment();
    
    //Payment Logic for a product purchase 
    
  }
  
}
```

{% endtab %}

{% tab title="LoginInterface & PaymentInterface" %}

```dart
void main(){
  
  
  var purchase1 = Purchase();
  
  
  purchase1.onlinepay(Bkash());
  purchase1.pay(Bkash());
  
}



abstract class LoginInterface{
  
  void login();
  
}

abstract class PaymentInterface{
  
  void payment();
  
  
}

class Bkash implements PaymentInterface,LoginInterface{
  
  
  @override
  void login(){
    
    print("login");
    
    // Login logic for Bkash
  }
  
  @override
  void payment(){
    
    print("Bkash");
    // Logic for payment using Bkash 
  }
  
}


class Rocket implements PaymentInterface,LoginInterface{
  
  
  @override
  void login(){
    
    print("login");
    
    // Login logic for Rocket 
  }
  
  @override
  void payment(){
    
    print("Rocket");
    // Logic for payment using Rocket
  }
  
}

class Visa implements PaymentInterface{
  
  @override
  void payment(){
    
    print("Visa");
    // Logic for payment using Visa
  }
  
}


class Cash implements PaymentInterface{
  
  @override
  void payment(){
    
    print("Cash");
    // Logic for payment using Cash 
  }
  
}

//--------------------


class Purchase{
  
  
  void onlinepay(LoginInterface paymenttype){
    
    
   paymenttype.login();
    
    
  }
  
  void pay(PaymentInterface paymenttype){
    
    
    
    paymenttype.payment();
    
    //Payment Logic for a product purchase 
    
  }
  
}
```

{% endtab %}
{% endtabs %}

![](/files/-MERyP8opUNPj-uhn9FM)

{% hint style="info" %}
Though dart gives us the flexibility to implement any class , most of the time you are going to implement  abstract classes containing abstract methods (i.e. turning those abstract classes into interfaces )&#x20;
{% endhint %}
