×

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 Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

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

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

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

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

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 Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

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

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 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 Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 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 Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.