Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the user with the blueprint of the class that any class should follow if it interfaces with that class. If a class inherits another class, it should override ( re-define ) the functions present inside that interfaced class in its own way as per the subject. In Dart, there isn't a specific or direct way of creating the interfaces. To implement them, we use the ' implement ' keyword. By default, every class is an interface in itself containing all the interface members and the members of any other interfaces that it implements.

Given below is the syntax of how to use interfaces in your programs :

Syntax :

class Interface_class_name
{
   // body of the class containing interface methods
}


class Class_name implements Interface_class_name 
{
   // body of the class overriding the interface methods of the interface class and containing its own methods
}

For a better understanding let’s have a look at one example that implements interface.

Example 1 :

// definition of the interface class ' Interface '
class Interface
{ 
   void display( ) 
   { 
      print( " This is the function of the interface class. " ) ; 
   } 
}  
  
// definition of the ' subclass ' implementing the interface class ' interface '
class subclass implements Interface 
{ 
   // overriding the display( ) function of the ' Interface ' class
   void display( ) 
   {  
      print( " This is the function of the subclass. " ) ; 
   } 
}
void main( ) 
{
    
   // creating object of the class ' subclass '
   subclass s1 = new subclass( ) ;
    
   // accessing the method display( ) of the ' subclass ' which has overriden this 
   // function of the ' Interface ' class
   s1.display( ) ; 
} 

Output :

This is the function of the subclass.

Note: ‘ implements ’ keyword should be used by the class in order to use the Interface class. By just inheriting ( extending ) the class, we cannot access the interface method.

Multiple Inheritance in Dart

Dart does not support multiple inheritance that is, the class inheriting from more that one child classes. To achieve multiple inheritance in Dart, we can use the concept of interfaces because Dart supports multiple interfaces.

Syntax :

class interface_class1 
{
    // body of the class containing interface methods
}
class interface_class2 
{
    // body of the class containing interface methods
}
.
.
.
.
class interface_classN 
{
    // body of the class containing interface methods
}


class class_name implements interface_class1, interface_class2, . . . . , interface_classN 
{
    // body of the class overriding the interface methods of the interface classes and containing its own methods
}

Example :

// definition of the interface class ' Interface1 '
class Interface1
{ 
   // definition of the display1( ) function of the ' Interface1 ' class
   void display1( ) 
   { 
      print( " This is the function of the interface class ' Interface1 '. " ) ; 
   } 
}  


// definition of the interface class ' Interface2 '
class Interface2 
{ 
  // definition of the display2( ) function of the ' Interface2 ' class 
  void display2( ) 
   { 
      print( " This is the function of the interface class ' Interface2 '. " ) ; 
   } 
}  


// definition of the interface class ' Interface3 '
class Interface3 
{ 
  // definition of the display3( ) function of the ' Interface2 ' class 
  void display3( ) 
   { 
      print( " This is the function of the interface class ' Interface3 '. " ) ; 
   } 
}  
  
// definition of the subclass implementing the above three interface classes
class subclass implements Interface1, Interface2, Interface3 
{ 
   void display1( ) 
   {  
      print(" Overriding the display1( ) function in the subclass. " ) ; 
   } 
    
  void display2( ) 
   {  
      print( " Overriding the display2( ) function in the subclass. " ) ; 
   } 
   void display3( ) 
   {  
      print( " Overriding the display3( ) function in the subclass. " ) ; 
   }   
} 


void main( )
{
   // creating the object of the subclass
   subclass s1 = new subclass( ) ;
    
   // calling overridden methods of the interface classes in the subclass
   s1.display1( ) ; 
   s1.display2( ) ; 
   s1.display3( ) ; 
}  

Output :

 Overriding the display1( ) function in the subclass. 
 Overriding the display2( ) function in the subclass. 
 Overriding the display3( ) function in the subclass. 

Importance of Interface :

  1. It can be used to implement one of the most important concept of object oriented feature – data abstraction.
  2. Dart doesn’t support multiple inheritance. However, using interfaces we can implement the multiple inheritance.

Important Points to remember :

  1. There is no explicit implementation or declaration of an interface class. So, the general definition of the class can be considered as the interface.
  2. In Dart, a class can extend only one class but can implement any number of the classes.
  3. If a class is implementing an interface, then it is mandatory to override the method and instance variable of that interface class.