Appendix B. Defining classes and libraries

 

This appendix discusses building classes and using libraries and privacy.

B.1. Classes and interfaces

Dart is a class-based, object-orientated language with single inheritance and multiple interfaces. Dart has explicit classes and implicit interfaces: that is, a class definition implicitly defines an interface on its public properties and methods that other classes can implement.

Note

In the initial release of Dart, there were explicit interfaces defined using the interface keyword. After feedback from Dart’s early adopters, it was discovered that because an abstract class definition also defines an interface, the interface keyword was redundant.

B.1.1. Defining classes

The class keyword is used to define a class. Classes must be defined in top-level scope (that is, you can’t define a class in a function, method, or other class):

You create an instance of a class by using the new keyword:

You use the class name as type information throughout your application when assigning an instance into a variable or defining a function parameter. Just as with the built-in types (String, int), using that type information is optional, but the Dart tools will validate your code if you annotate your variables and parameters with type information:

Properties

B.2. Libraries and privacy