Interface in Dart

·

4 min read

Cover Image for Interface in Dart

When any class implements an Interface, 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 an interface in a dart. Any class can act as an interface. Classes can use the 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

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

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

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 Employees {
  String profession;

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

class Jay implements Person, Employees {
  @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(); 
  Employees employees = new Employees(); 

  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(""); 

  employees.profession = "Chemical Engineer"; 
  employees.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 Employees my profession is Chemical Engineer

Conclusion

In Dart, interfaces are not explicitly declared with a separate syntax, unlike some other languages. Instead, any class can serve as an interface. When a class is used as an interface, other classes can use the "implements" keyword to adhere to that interface.

Key points:

  1. In Dart, interfaces are not declared separately but are implicit in class declarations.

  2. Any class can act as an interface in Dart.

  3. When a class implements an interface, it is required to override all methods and instance variables defined in that interface.

  4. Dart doesn't support multiple inheritance, but it allows a class to implement multiple interfaces, offering similar functionality without the complexities of multiple inheritance.

In essence, Dart uses class declarations to define interfaces, and classes implementing these interfaces must fulfil the contract by implementing all the required methods and instance variables. Multiple interfaces can be implemented by a single class for added flexibility.

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 on the first attempt. Even I get it on 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 the 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 hesitant 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