thumbnail

Abstract Methods in Dart

Published on 29 March 2020
Last Updated on 29 March 2020

Abstract methods are defined and encapsulated inside an abstract class. Abstract methods can be used for defining an interface but leaving its implementation up to other classes.

Abstract methods only define an interface but do not contain any execution details.

Abstract methods do not contain any execution statements.

Refer to following program to know how abstract methods are defined.

Program to demonstrate use of abstract methods in Dart

// program to demonstrate use of abstract methods in Dart

abstract class Dog {
  // define various instance variables and methods that abstract class Dog must have

  String name;

  // following methods must be implemented by the child class
  void bark();
  void eat();
  void sleep();
  void jump();
  void walk();
}

class Tommy extends Dog {
  String name = 'Tommy';

  void bark() {
    print('${name} is barking!');
  }

  void eat() {
    print('${name} is eating!');
  }

  void sleep() {
    print('${name} is sleeping!');
  }

  void jump() {
    print('${name} is jumping!');
  }

  void walk() {
    print('${name} is walking!');
  }
}

main(List<String> args) {
  var dog = new Tommy();
  dog.bark();
  dog.eat();
  dog.sleep();
  dog.jump();
  dog.walk();
}

Above program produces following output:

Tommy is barking!
Tommy is eating!
Tommy is sleeping!
Tommy is jumping!
Tommy is walking!

How to fix missing concrete implementation error

If you extending from an abstract class as shown above, however if you do not implement all the methods that abstract class requires you to implement then dart analysis will report an error telling you to implement missing concrete implementations of the abstract methods.

Once you have implemented the abstract method that abstract class requires you to implement then the error will be gone.