Add Your Heading Text Here

Generally, dart interface is used for as a replacement for multiple inheritance.

In dart, interface has the similar structure as class. Interface in dart defined using class keyword.

So, you will think that how an interface is different than class in dart?

Actually, interface in dart has been removed and the concept of interface in dart is optional(like no significance meaning in dart).

In dart, all classes are implicit interfaces which means classes itself act as interface.

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.

In dart, An interface is a class which can be inherited by another class using implements keyword.

Methods inside interface should have body in dart language.

Any class which implements the interface must have to implement all the methods of that particular interface.

For ex :

class ButtonClickListener{
	void onButtonClick(){
    print("Default Button Clicked !!");	
  }
}
class Login implements  ButtonClickListener{
// implement all the methods of  ButtonClickListener
	onButtonClick(){
		print("Login Button Clicked !!");	
	}
}
class Signup implements  ButtonClickListener{
// implement all the methods of  ButtonClickListener
  onButtonClick(){
		print("Signup Button Clicked !!");	
	}
}
void main(){
// you can create an object of a class which act as implicit interface
  ButtonClickListener listener=new ButtonClickListener();
  listener.onButtonClick();
  Login login=new Login();
  login.onButtonClick();
  Signup signup=new Signup();
  signup.onButtonClick();
}

Output :

Default Button Clicked !!
Login Button Clicked !!
Signup Button Clicked !!

As dart is an optionally typed language, we don’t need to use interfaces. Just by writing appropriate methods with proper signature in classes is enough to fulfill your requirements.

Personally, i don’t suggest to focus interface in dart labguage as they do not have special case in dart language.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons