Original text: medium.com/@manoelsrs/…

Stackoverflow.com/questions/4…

Use the extends

Inheritance allows us to create new classes that can be used to reuse, extend, or modify existing class behavior. In Dart, only one superclass can be inherited, but inheritance is transitive.

class Vehicle {
  Vehicle(this.color);

  final String color;
  final String definition = 'Vehicle';
}

class Car extends Vehicle {
  Car(String color) : super(color);
}

class Hatch extends Car {
  Hatch(String color) : super(color);
}

main() {
  final hatch = Hatch('red');
  print('Result: ${hatch is Vehicle}');
  // Output -> Result: true
}
Copy the code

When Class Car extends Vehicle, all properties, variables, and methods implemented in class Vehicle are available to class Car.

main() {
	final car = Car('red');
    print('Result: Car is a ${car.definition}');
    // Output -> Result: Car is a Vehicle
}
Copy the code

It is also possible to override methods of a superclass:

class Car extends Vehicle {
	Car(String color) : super(color);
    
    @override
    String get definition => 'Car The ${super.definition}';
}
Copy the code

So, use extends when you want to create a more specific version of a class.

Using implements

If you want to create your own Car class, you don’t want to inherit all the attributes, variables, and methods of the Vehicle class, but only the Vehicle type. In this way, the Car class implements the Vehicle interface.

class Car implements Vehicle {
	Car(this.carColor);
    
    final String carColor;
    
    @override
    String get color => carColor;
    
    @override
    String get definition => '$carColor car';
}

main() {
	final car = Car('red');
    print('Result: definition: ${car.definition}');
    // Output -> Result: definition: red car
    print('Result: is Vehicle type: ${car is Vehicle}');
    // Output -> is Vehicle type: true
}

Copy the code

Dart allows you to implement multiple classes or interfaces;

☝ ️ example

Suppose you want to implement a bird and duck class. Both are animals, but the bird can fly and the duck can both fly and swim. How do you do that?

First, create the superclass and behavior:

class Animal {}

abstract class Flyer {
	void fly() => print('I can fly! ');
}

abstract class Swimmer {
	void swim() => print('I can swim! ');
}
Copy the code

Now, create Bird and Duck to implement the appropriate behavior:

class Bird extends Animal implements Flyer {
	@override
    void fly() => print('I can fly! ');
}

class Duck extends Animal implements Swimmer.Flyer {
	@override
    void fly() => print('I can fly! ');
    
    @override
    void swim() => print('I can swim! ');
}
Copy the code

Implements fly and Swim methods. This way there is no way to inherit the implementation of the behavior. Is there a way to reuse the implementation code?

Use with

Mixins are a different type of structure that can only be used with the keyword with. Mixins are used to contain common snippets of code, that is, reusable code.

Mixins are a way to reuse code across multiple class hierarchies.

class Animal {}

abstract class Flyer {
	void fly() => print('I can fly! ');
}

abstract class Swimmer {
	void swim() => print('I can swim! ');
}

class Bird extends Animal with Flyer {}

class Duck extends Animal with Swimmer.Flyer {}

Copy the code

You can replace abstract class with mixin.

Mixins are a way of abstracting and reusing a set of operations and states, much like the way code is reused when extends is a class, except instead of multiple inheritance, there is only one superclass. In the above example, mixin’s methods can also be overridden in a class.

Summary

Mixins focus on how a class does what it’s supposed to do, inheriting and sharing concrete implementations.

Implements focuses on what a class is, it’s an abstract signature and a promise that the class must fulfill.