Dart: Interfaces

class identifier implements interface_name

Example

void main() { 
   ConsolePrinter cp= new ConsolePrinter(); 
   cp.print_data(); 
}  
class Printer { 
   void print_data() { 
      print("This is a cat"); 
   } 
}  
class ConsolePrinter implements Printer { 
   void print_data() {  
      print("This is a dog"); 
   } 
} 

=> output

This is a dog

Implementing Multiple Interfaces

class identifier implements interface-1,interface_2,interface_3, ...

Example

void main() { 
   Calculator c = new Calculator(); 
   print("The gross total : ${c.ret_tot()}"); 
   print("Discount :${c.ret_dis()}"); 
}  
class Calculate_Total { 
   int ret_tot() {} 
}  
class Calculate_Discount { 
   int ret_dis() {} 
}
class Calculator  implements Calculate_Total,Calculate_Discount { 
   int ret_tot() { 
      return 50; 
   } 
   int ret_dis() { 
      return 10; 
   } 
}

=> output

The gross total: 50
Discount:10

Leave a Reply

Your email address will not be published. Required fields are marked *