Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page
  • Syntax & Properties of interface
  • Interface : Why we use them -1 ?
  • Interface : Why we use them -2 ?

Was this helpful?

  1. Dart Fundamentals-2

Interface

PreviousAbstract Class and Abstract MethodsNextMiscellaneous concepts

Last updated 4 years ago

Was this helpful?

Syntax & Properties of interface

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() {}
}

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() {}
}

You can implement multiple interfaces at once

Whenever you implement a interface you must override everything inside it

Interface : Why we use them -1 ?

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);

Interface : Why we use them -2 ?

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 
    
  }
  
}
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 
    
  }
  
}

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 )

Dart implicitly defines a class as an interface, hence called as . As there is no interface keyword , implicitly all classes are interfaces . Note: This might change in the future

implicit interface