What is Interface in DART

In this article, you will learn how to use interface in dart
  • 3379

Dart Interface

An interface defines how one may interact with an object. In interface you can declare a abstract method (means you can declare body in it's sub class ), basically it is used for solving  the problem of multiple inheritance.

Syntax of interface

interface identifier interface name{ //.........Abstract Method.................//} e.g, interface MyInterface{ AbstactMethod();}


Example of interface
 

interface Dartinterface{

  MyInterface();

}

class BasicInterface implements Dartinterface{

  MyInterface(){

    print("I am a inteface");

  }

}

void main(){

  BasicInterface obj = new BasicInterface();

  obj.MyInterface();
}


Output:

defaultinterface.png

  • Default class for an interface

    It is a new feature then other object oriented programming language, the example of default class for an interface is given below

Example of default class for an interface
 

interface Dartinterface default BasicInterface{

  MyInterface();

  Dartinterface();

}

class BasicInterface implements Dartinterface{

  MyInterface(){

    print("I am a inteface");

  }

}

void main(){

  //BasicInterface obj = new BasicInterface();

  Dartinterface obj = new Dartinterface();

  obj.MyInterface();
}

Output:

defaultinterface.png

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2020 DotNetHeaven. All rights reserved.