×

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 Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 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.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 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.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

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.

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.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

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.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.