Was the "interface" keyword removed from Dart?
Asked Answered
G

3

12

Just to be sure, has Dart removed explicitly defining an interface now in favor of implicitly defining it via abstract?

I see it mentioned in Dart and Interface Segregation Principle, however I'm also finding a lot of content still referencing the explicit definition, such as When to use interfaces in Dart?

Groat answered 18/2, 2015 at 22:52 Comment(0)
G
26

Yes. The interface keyword was removed from Dart. Instead all classes have implicit interfaces. So if you want to define an interface you can use an abstract class instead.

See this blog post from 2012 about eliminating the interface keyword.

Goldina answered 18/2, 2015 at 23:4 Comment(1)
That was absolutely correct when it was written - but no longer applies since Dart 3Downtrend
D
8

Please note that the accepted answer is no longer true.

Dart 3 has re-introduced the interface keyword. See here: https://dart.dev/language/class-modifiers#interface

The semantic of this differs from what you might expect from your background in other languages like Java: In Dart, this is only a class modifier that ensures that code in other files (= "libraries" in Dart) can not extend this class but must implement it completely (or be abstract).

To get Java-like "pure" interfaces, use abstract interface

Downtrend answered 19/6, 2023 at 15:46 Comment(0)
C
1

Using interface modifier allows the class to be used in an implements clause, but prevents it from being used in extends.

It was reintroduced but I think in different context. You can checkout below link for more details.

Interface Modifier Usecase

Chatwin answered 11/2, 2024 at 11:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.