Classes and objects
Learning objectives
- You practice working with classes and objects in Dart.
- You know how to define classes in Dart and know the basic object-related terminology.
Dart is an object-oriented programming language. Like many object-oriented programming languages, it offers ways to couple concepts together as well as to create entities that are used to divide and conquer aspects of a program.
Defining a class
Classes in Dart are defined using the keyword class
, which is followed by the name of the class and the block that contains the class definition. Naming of classes -- and more broadly types -- follows UpperCamelCase
, i.e. the first character of the name is also written in uppercase.
The following example defines a class called Person
that has a name and a year of birth. The name and year of birth are instance properties. The class also has a constructor Person
which is used to assign the values for the instance properties.
class Person {
String name;
int yearOfBirth;
Person(this.name, this.yearOfBirth);
}
When defining instance properties, we also define their types. Above, the name is a String and the year of birth is an integer.
Creating an object
Creating an object is done by calling a constructor. Above, we defined a constructor that takes the name and the year of birth as a parameter, which we can now call.
void main() {
final person = Person("Artturi Iivari Virtanen", 1895);
}
class Person {
String name;
int yearOfBirth;
Person(this.name, this.yearOfBirth);
}
Unlike e.g. in Java, a new
keyword is not used when calling a constructor.
Instance properties
Each object has its own values for the instance properties. Instance properties of an object are accessed using the dot notation, where the name of the object comes before the dot and the name of the instance property comes after the dot.
The following example creates two objects and prints their names.
Instance methods
Instance methods -- i.e. kind-of-functions that have access to the instance properties of the object that the method is invoked on -- are defined within the block of a class. In the following example, we define an instance method that is used to determine whether the person has been born after another person.
The example also demonstrates defining the type of return values as well as the type of the parameters. If we would wish to be explicit about instance properties, we could use the keyword this
, writing the function bornAfter
as follows.
bool bornAfter(Person person) {
return this.yearOfBirth > person.yearOfBirth;
}
Method toString
Similar to e.g. Java, Dart has a default method called toString
that can be used to create String-based representations of objects.
The @override
annotation is syntactic sugar that describes that we are overriding a method -- in this case from the class Object that all Dart classes inherit. It is also helpful; if we annotate a method with @override
that does not actually override a method from the parent class, the programming environment will indicate this, helping us e.g. with potential typos.