An Tran
An Tran

Reputation: 89

How do you perform inheritance with abstract classes in dart? error : superclass SpanishData doesn't have a zero argument constructor

I'm trying to create an abstract class called SpanishData And then I want to create another class called alphabet that extends Spanish data

I'm getting an error: the superclass SpanishData doesn't have a zero-argument constructor. How do I fix this?

Here is my code:

abstract class SpanishData{
  String englishWord;
  String spanishWord;
  String mp3;

  SpanishData(this.englishWord,this.spanishWord,this.mp3);

  void getList (){

  }



}


//the alphabet class

import '../SpanishDataAbstract.dart';


class Alphabet extends SpanishData{

  @override
  void getList(

      )


}

Upvotes: 4

Views: 2356

Answers (1)

timilehinjegede
timilehinjegede

Reputation: 14043

You need to refer to the properties of the parent class your class is extending. You can do this using the super keyword.

The super() method on a class constructor allows a subclass to pass arguments and execute the constructor of its superclass. 

The code below works:

abstract class SpanishData{
  String englishWord;
  String spanishWord;
  String mp3;

  SpanishData(this.englishWord,this.spanishWord,this.mp3);

  void getList (){

  }
}


class Alphabet extends SpanishData{

  // create a constructor of the alphabet class and call the parent constructor
  Alphabet(String englishWord, String spanishWord, String mp3) : super(englishWord, spanishWord, mp3);

  @override
  void getList(){}
}

Upvotes: 6

Related Questions