voidmain() {// Interfaces in dart}abstractclassA {String a;voidtestA1();voidtestA2() {print("A2"); }}abstractclassB {voidtestB1();voidtestB2() {print("B2"); }}classCimplementsA, B {@overridevoidtestB1() {}@overridevoidtestB2() {}@overrideString a;@overridevoidtestA1() {}@overridevoidtestA2() {}}
voidmain() {// Interfaces in dart}classA {String a;voidtestA2() {print("A2"); }}classB {voidtestB2() {print("B2"); }}classCimplementsA, B {@overridevoidtestB2() {}@overrideString a;@overridevoidtestA2() {}}
Dart implicitly defines a class as an interface, hence called as implicit interface . As there is no interface keyword , implicitly all classes are interfaces .
Note: This might change in the future
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';voidmain() {// 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 doublearea(Shape shape){return shape.calarea(); }print(area(Square(10)));}abstractclassShape {voiddraw();doublecalarea();}classCircleimplementsShape{int radius;@overridevoiddraw() {print("Drawing a Circle"); }@overridedoublecalarea() {return pi*pow(this.radius,2); }Circle(this.radius);}classSquareimplementsShape {double length;@overridevoiddraw() {print("Drawing a Square"); }@overridedoublecalarea() {returnpow(this.length, 2); }Square(this.length);
Interface : Why we use them -2 ?
voidmain(){var purchase1 =Purchase(); purchase1.pay(Bkash());}abstractclassPaymentInterface{voidpayment();}classBkashimplementsPaymentInterface{@overridevoidpayment(){print("Bkash");// Logic for payment using Bkash }}classRocketimplementsPaymentInterface {@overridevoidpayment(){print("Rocket");// Logic for payment using Rocket }}classVisaimplementsPaymentInterface{@overridevoidpayment(){print("Visa");// Logic for payment using Visa }}classCashimplementsPaymentInterface{@overridevoidpayment(){print("Cash");// Logic for payment using Cash }}//--------------------classPurchase{voidpay(PaymentInterface paymenttype){ paymenttype.payment();//Payment Logic for a product purchase }}
voidmain(){var purchase1 =Purchase(); purchase1.onlinepay(Bkash()); purchase1.pay(Bkash());}abstractclassLoginInterface{voidlogin();}abstractclassPaymentInterface{voidpayment();}classBkashimplementsPaymentInterface,LoginInterface{@overridevoidlogin(){print("login");// Login logic for Bkash }@overridevoidpayment(){print("Bkash");// Logic for payment using Bkash }}classRocketimplementsPaymentInterface,LoginInterface{@overridevoidlogin(){print("login");// Login logic for Rocket }@overridevoidpayment(){print("Rocket");// Logic for payment using Rocket }}classVisaimplementsPaymentInterface{@overridevoidpayment(){print("Visa");// Logic for payment using Visa }}classCashimplementsPaymentInterface{@overridevoidpayment(){print("Cash");// Logic for payment using Cash }}//--------------------classPurchase{voidonlinepay(LoginInterface paymenttype){ paymenttype.login(); }voidpay(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 )