DEV Community

Cover image for Interface in Dart
Jay Tillu
Jay Tillu

Posted on • Updated on

Interface in Dart

  • The interface defines the syntax that any class must follow. Interface mostly used to apply compulsion on class.

  • When any class implements an Interface then it must override every method and instance variable of an interface.

  • However, Dart does not have a syntax for declaring interfaces. The class declaration is themselves interface in a dart.

  • Any class can be act as an interface. Classes can use implements keyword to use any class as an interface.

  • Once any class is used as interface then it is mandatory to override each and every method and instance variable of that class. In simple words, a class must redefine every method and instance variable of the interface.

Syntax of declaring Interface

class class_name implements interface_name
Enter fullscreen mode Exit fullscreen mode

Sample Code

class Person {

  void walk() {
    print("Person can walk");
  }

  void talk() {
    print("Person can talk");
  }
}
class Jay implements Person {

  @override
  void walk() {
    print("Jay can walk");
  }

  @override
  void talk() {
    print("Jay can talk");
  }
}

main() {
  Person person = new Person();
  Jay jay = new Jay();

  person.walk();
  person.talk();

  jay.walk();
  jay.talk();
}

Output
Person can walk
Person can talk
Jay can walk
Jay can talk
Enter fullscreen mode Exit fullscreen mode

Implementing Multiple Interface


  • Dart does not support multiple inheritance but dart can implement multiple interfaces. To provide similar power and functionality.

The syntax for declaring multiple interfaces

class class_name implements interface1, interface2, interface3
Enter fullscreen mode Exit fullscreen mode

Sample Code

class Person {
  String name;

  void ShowName() {
    print("My name is $name");
  }

  void walk() {
    print("Person can walk");
  }

  void talk() {
    print("Person can talk");
  }
}

class Akshay {
  String profession;

  void ShowProfession() {
    print("from class Akshay my profession is $profession");
  }
}

class Jay implements Person, Akshay {
  @override
  String profession;

  @override
  void ShowProfession() {
    print("from class Jay my Profession is $profession");
  }
@override 
  String name; 

  @override 
  void ShowName() { 
    print("From class Jay my name is $name"); 
  } 

  @override 
  void walk() { 
    print("Jay can walk"); 
  } 

  @override 
  void talk() { 
    print("Jay can talk"); 
  } 
} 

main() { 
  Person person = new Person(); 
  Jay jay = new Jay(); 
  Viraj viraj = new Viraj(); 

  person.walk(); 
  person.talk(); 
  person.name = "Person"; 
  person.ShowName(); 

  print(""); 

  jay.walk(); 
  jay.talk(); 
  jay.name = "JAY"; 
  jay.profession = "Software Development"; 
  jay.ShowProfession(); 
  jay.ShowName(); 

  print(""); 

  akshay.profession = "Chemical Engineer"; 
  akshay.ShowProfession(); 
}

Output

Person can walk
Person can talk
My name is Person

Jay can walk
Jay can talk
from class Jay my Profession is Software Development
From class Jay my name is JAY

from class Akshay my profession is Chemical Engineer
Enter fullscreen mode Exit fullscreen mode

So that’s it for interface guys. Please read this article twice if you don’t understand the concept in the first move. And do a little bit of research on an interface. Don’t worry if you didn’t get the concept in the first attempt. Even I get it in a 3rd try. So don’t worry, just practice it.

Also, feel free to let me know if I miss something. Till then keep Loving, keep Coding. And I’ll surely catch you up in next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn more about Dart and Flutter

Follow me for more such content

Top comments (0)