Search code examples
oopdartabstract-classbase-class

Abstract base class in Dart


I have been programming in Java for nearly two years but I am now more shifting to web programming and thus to Javascript, or in my case to Dart. For a project I'm working on I would like to have abstract base classes, just I would have in Java. I have been looking on the internet but I can't find anything on abstract classes at all for Dart. I only found this article from the dart lang site on mixins, that in an example uses the abstract keyword for a class. But I don't really understand the mixins principle.

Could somebody translate this easy Java abstract base class example to Dart so that I can have a basic understanding on how it would be done in Dart? The example covers abstract base class (ofcourse, with abstract methods), polymorphism, casting objects, method overloading (in this case it is the constructor), calling super-constructor and calling overloaded own constructor.

// Abstract base class
abstract class Vehicle {
    protected final int maxSpeed;
    protected int speed;
    
    Vehicle() {
        this(0);
    }
    
    Vehicle(int maxSpeed) {
        this.maxSpeed = maxSpeed;
        speed = 0;
    }
    
    public int getMaxSpeed() {
        return maxSpeed;
    }
    
    abstract void accelerate();
    abstract void brake();
}

// Subclass of Vehicle, the abstract baseclass
class Car extends Vehicle {
    public final int doors;
    
    Car(int maxSpeed, int doors) {
        super(maxSpeed);
        this.doors = doors;
    }
    
    @Override
    void accelerate() {
        if (speed>maxSpeed) {
            speed = maxSpeed;
            return;
        }
        speed += 2;
    }

    @Override
    void brake() {
        if (speed - 2 < 0) {
            speed = 0;
            return;
        }
        this.speed -= 2;
    }
}

And how would this easy implementation look like?

// Polymorphism
Vehicle car = new Car(180, 4);

// Casting
int doors = ((Car)car).doors;

// Calling abstract method
car.accelerate();

Solution

  • Actually it does become simpler in dart (https://dartpad.dartlang.org/37d12fa77834c1d8a172)

    // Abstract base class
    abstract class Vehicle {
      final int maxSpeed;
      int speed = 0;
    
      Vehicle([this.maxSpeed = 0]);
    
      void accelerate();
      void brake();
    }
    
    // Subclass of Vehicle, the abstract baseclass
    class Car extends Vehicle {
      final int doors;
    
      Car(int maxSpeed, this.doors) : super(maxSpeed);
    
      @override
      void accelerate() {
        if (speed > maxSpeed) {
          speed = maxSpeed;
          return;
        }
        speed += 2;
      }
    
      @override
      void brake() {
        if (speed - 2 < 0) {
          speed = 0;
          return;
        }
        this.speed -= 2;
      }
    }
    
    main() {
      // Polymorphism
      Vehicle car = new Car(180, 4);
    
      // Casting
      int doors = (car as Car).doors;
    
      // Calling abstract method
      car.accelerate();
    }