Abstract classes (and interfaces)

on Sunday, 19th of July, 2020

An abstract class, which is similar to an interface, defines a class that cannot be instantiated.

Abstract classes keep your code honest and type safe. It ensures that all implementation subclasses define all the properties and methods that abstract class defines, but leaves the implementation to each subclass.

abstract class Animal {
  int age;
  String name;

  // no method body means you're defining an abstract method
  // abstract methods must be overridden in implementation classes
  void talk();

  // abstract classes _can_ implement some functionality. 
  // when the method functionality is written on the abstract class,
  // the implementation subclasses don't have to override it.
  void growl() => print('grrrrr');
}

class Cat implements Animal {
  
  int age;

  
  String name;

  Cat(this.name, this.age);

  
  void talk() {
    print('meow');
  }
}

class Dog implements Animal {
  
  int age;

  
  String name;

  Dog(String name, int age);

  
  void talk() {
    print('bark');
  }
}

// This method expects an Animal instance as an argument
// But, Animal is _abstract_, which means you cannot instantiate it directly
// So, any class that _implements_ Animal is guaranteed to have
// overriden the methods and properties on the Animal interface, 
// which makes it 'type safe'.
void makeAnimalNoise(Animal animal) => animal.talk();

main() {
  final cat = Cat('Nora', 5);
  final dog = Dog('Pepperstake', 1);
  makeAnimalNoise(cat);
  makeAnimalNoise(dog);

  dog.growl();
  cat.growl();
}


Join thousands of Flutter developers.

Sign up for infrequent updates about Flutter and Dart.

You can get all this content and more in one place. Check out my new book Flutter in Action