Dart Tutorial - Inheritance using class, interfaces and mixin

Rahul Vashishth
learn dart
Published in
3 min readMay 20, 2019

Dart class is same as Java classes. We can create static and instance members, can extend another class, can implement an interface. But this is not the all the dart have. Dart provides a kind of multiple inheritance concept using mixins, will reach there shortly.

Inheritance by extending a class

Let’s first understand the inheritance by extending a class. As simple as any major oops programming language. In Dart we can extend another class by extending that class. Consider following example.

class Car {
void accelerate(){
print("simple acceleration");
}

void brakes(){
print("simple brakes");
}
}
class Toyota extends Car {
@override
void brakes(){
print("toyota disk brakes");
}
}
void main(){
Toyota toyota = new Toyota();
toyota.accelerate();
toyota.brakes();
}
//output
simple acceleration
toyota disk brakes

Inheritance using interface

Now the inheritance using interface. Every dart class is also an interface(implicitly). Dart don’t have a separate keyword for an interface, instead we can simply implement a class. Most of the rules are same as java.

When implementing an interface, either provide implementation for all methods or make your calls an abstract one.

class Car {
void accelerate(){
print("simple acceleration");
}

void brakes(){
print("simple brakes");
}
}
class Toyota implements Car {void brakes(){
print("toyota disk brakes");
}

void accelerate(){
print("toyota power acceleration");
}
}
void main(){
Toyota toyota = new Toyota();
toyota.accelerate();
toyota.brakes();
}
// output
toyota power acceleration
toyota disk brakes

One interface can implement multiple interface.

Mixins based inheritance

Now the most interesting part. Mixins based inheritance. Mixins is basically a class, whose method can be extended by other classes without extending the class itself.

class Car {
void accelerate(){
print("simple acceleration");
}

void brakes(){
print("simple brakes");
}
}
mixin EuroVICar {
void emission(){
print("euro vi standard emission");
}
}
class Toyota with EuroVICar implements Car {void brakes(){
print("toyota disk brakes");
}

void accelerate(){
print("toyota power acceleration");
}
}
void main(){
Toyota toyota = new Toyota();
toyota.accelerate();
toyota.brakes();
toyota.emission();
}
//
toyota power acceleration
toyota disk brakes
euro vi standard emission

Here emission method is used by Toyota class like it is it’s own. Also we can omit the use of mixin keyword.

Any class can behave as mixin as long as it don’t extend any other class and don’t have constructors.

We can also have a class with multiple mixins. Also we can restrict a mixin to be used by certain classes.

Mixin class are like abstract class. But you can not extend a mixin.

class Car {
void accelerate(){
print("simple acceleration");
}

void brakes(){
print("simple brakes");
}
}
mixin EuroVICar {
void emission(){
print("euro vi standard emission");
}
}
// mixin using class keyword
class Autonomus {
// don't declare a constructor if you want to use this
// class as a mixin type
void autonomusDrive(){
print("autonomus drive capable ");
}

}
// This class is using two mixins
class Toyota with EuroVICar, Autonomus implements Car {
void brakes(){
print("toyota disk brakes");
}

void accelerate(){
print("toyota power acceleration");
}
}
void main(){
Toyota toyota = new Toyota();
toyota.accelerate();
toyota.brakes();
toyota.emission();
toyota.autonomusDrive();
}
//output
toyota power acceleration
toyota disk brakes
euro vi standard emission
autonomus drive capable

That’s pretty much to get started with inheritances in dart. Ask a question in the comments if any.

--

--

Rahul Vashishth
learn dart

Tech Enthusiast, Cloud, Kubernetes, Java, DevOps. Environmentalist.