loading...
Cover image for Interface in Dart

Interface in Dart

jay_tillu profile image Jay Tillu😎 ・3 min read
  • 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

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

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

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

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

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

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.

For More Information Please Visit Following Links

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊
Twitter
Facebook
Instagram
Medium
or just mail me at jayviveki13@gmail.com

Posted on by:

jay_tillu profile

Jay Tillu😎

@jay_tillu

🌏 विधायास्य धमॅस्य संरक्षणम् 🌍 ☀️ Society before Self ☀️ 🙏🏻 Humanism by Religion 🙏🏻 💻 Work for Mankind 💻 🌐 Believes in वसुधैव कुटुम्बकम् 🌐

Discussion

pic
Editor guide