×

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.

Related Topics

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.