A Beginners Guide to Dart - Intro to Classes and Objects - Part Three
What Will I Learn?
- You will learn about Class Constructors and Named Constructors
- You will learn about Getters and Setters
- You will learn about Class Fields
- You will learn about Basic Inheritance
- You will learn about Operator Methods
- You will learn how to Override Methods
OS Support for Dart:
- The Dart SDK
- A dart supported text editor
- A little time to sit and watch a video and some patience to learn the language
Resources for Dart:
- Dart Website: https://www.dartlang.org/
- Dart Official Documentation: https://www.dartlang.org/guides/language/language-tour
- Dart GitHub repository: https://github.com/dart-lang/sdk
- Awesome Dart GitHub Repository: https://github.com/yissachar/awesome-dart
- Pub website: https://pub.dartlang.org
Dart Logo (Google): https://www.dartlang.org/
In this Dart Video tutorial, we take a look at Objects and Classes. We look at how you can create a class as a blue print or set of rules for how to define the behavior and data of an object. We look at class constructors, named constructors, getter functions, setter functions and fields. We also take a look at operator methods and how we can override other methods from parent classes in our own custom class data types. Through all of this, we also talk about basic inheritance.
Class Constructors and the Class interface
When you build a class in any object oriented programming language, you need a way to instantiate the class into an object. Because the class defines how the object should hold data and how it should behave, the constructor is also paramount to this process. In Dart, we have the primary constructor which is generally used to populate the fields in the object and we can create named constructors which allow us to define further constants on the creation of our custom objects. These constructors are the main entry point for how the Class/Object interfaces with the rest of the language.
In this image, we have the primary constructor at the top. This constructor initializes both the real and imaginary values of the
Complexobject. This is important, because both of these values must contain some value to create a true Complex Number. The named constructors,
Complex.imaginaryboth make use of the initializer operation. They use this operation to call the primary constructor with a specific set of rules.
Complex.realpopulates the imaginary field with a zero and
Complex.imaginarypopulates the real field with a zero. In this way, we can setup all types of complex numbers without having to always pass in two parameters.
Inheriting from the Object Type and Overriding its Behavior
As mentioned a few times in this tutorial series, all classes and objects in Dart a children of the Object type. This is an important abstraction because the Object type defines a set of properties and behaviors that all classes and objects must have. In this video, we look at the
toString() method and the
== operator specifically but the Object type also includes a few more important properties. These properties will automatically be applied to the child Objects unless the developer specifically overrides them with new behaviors.
In this image, an example is shown where we override both the
== methods so that they fit the Complex Number abstraction better. The
toString() method is called every time the object needs to be stringified. This includes when the object is printed to the console via the
real + imaginary i format. If it is negative, we print it with a
real - imaginary.abs() i format. On the other hand, the
== operator uses the real and imaginary fields to find out if the object is numerically equivalent to another object. If the object that we are comparing to is not a
Complex object type, then it will always return
false. If it is a
Complex object type, then it will compare the real numbers and the imaginary numbers to one another.
The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-2