Dart inheritance and super constructor

Viewed 3604

I am trying to define an abstract parent class Event and a concrete child PlaceChangeEvent:

abstract class Event {
    String name;
    DateTime occurredAt;
    int handleCount;

    Event(this.name, this.occurredAt, [this.handleCount = 0]);
}

class PlaceChangeEvent implements Event {
    Place place;

    PlaceChangeEvent(Place place) : super(place.name, DateTime.now());
}

The PlaceChangeEvent is giving me 2 compiler warnings:

  • The first is on the class name itself: Missing inherited members: 'Event.name', 'Event.handleCount' and 'Event.occurredAt'
  • The second is on the call to super: 0 positional arguments expected, but 2 found

Where did I go wrong and what can I do to fix this? My intentions are to:

  • Hardcode the parent's name with the place.name; and
  • Hardcode the parent's occurredAt property with the current date/time'; and
  • Initialize the parent's optional handleCount property to 0 by simply not providing it.
3 Answers

I know this question is old and provided answer is good, should be accepted. So, here an example on difference between implements and extends:

import 'dart:math';

abstract class Shape {
  num get area;

  @override String toString() => 'Area is ${this.area}';
}

class Square extends Shape {
  final num side;
  Square({this.side: 2});

  num get area => this.side * this.side;
}

class Circle implements Shape {
  final num radius;
  Circle(this.radius);
  num get area => PI * pow(radius, 2);
}

void main() {
  print(new Square());
  print(new Circle(4));
}

And when you run it:

dart "./dart-tut/shape.dart"
Area is 4
Instance of 'Circle'
Related