Flutter for Beginners
上QQ阅读APP看书,第一时间看更新

Introduction to OOP in Dart

In Dart, everything is an object, including the built-in types. Upon defining a new class, even when you don't extend anything, it will be a descendant of an object. Dart implicitly does this for you.

Dart is called a true object-oriented language. Even functions are objects, which means that you can do the following:

  • Assign a function as a value of a variable.
  • Pass it as an argument to another function.
  • Return it as a result of a function as you would do with any other type, such as String and int.

This is known as having first-class functions because they're treated the same way as other types.

Another important point to note is that Dart supports single inheritance on a class, similar to Java and most other languages, which means that a class can inherit directly from only a single class at a time.

A class can implement multiple interfaces and extend multiple classes  using  mixins, which we will cover later in this chapter.

Here are the main OOP artifacts that are presented in the Dart language (we will delve deeper into each throughout this chapter):

  • Class: This is a blueprint for creating an object.
  • Interface: This is a contract definition with a set of methods available on an object. Although there is no explicit interface type in Dart, we can achieve the interface purpose with abstract classes.
  • Enumerated class: This is a special kind of class that defines a set of common constant values.
  • MixinThis is a way of reusing a class's code in multiple class hierarchies.