# 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  .**\
\&#xNAN;***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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-1/interface.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
