Krupal Shah Krupal Shah - 3 months ago 7
Dart Question

Understanding 'implements' and 'with' in Dart

I am just reading Dart language specification and exploring a new interesting language. As Dart language specification says: Dart has

implicit interfaces
. Which means every
class
is an
interface
too. So, If I want to implement some behavior of another class,
implements
clause is the only I need.

Also, Dart supports
mixins
. So that we can take implementation of methods from another class using
with
keyword.

So, given that if an abstract class A defines method a() like :

abstract class A {
void a();
}


and another two concrete class B defines method a() but does not implements class A like:

class B {
void a() {
print("I am class B");
}
}


and class C implements class A with Mixin B like :

class C extends Object with B implements A {
...
}


Here, I have few questions about it. If a class implements the interface and also use mixin that has method implementation with same method name; doesn't it would make
cycling inheritance
possible?
What will be the behaviour of
class C
? Does it need to implement
a()
or it will be implicitly implemented by
mixin B
?

I am just learning Dart and concepts like mixins are very unfamiliar to me. Can anyone help me understanding by answering my questions?

Answer

Mixins are a kind of limited multiple inheritance. With C with B, C inherits an implementation of void a(). Adding implements A doesn't need anything more to be done, because C already fulfills the contract it claims to fulfill by implements A, because of B.

Comments