A Beginners Guide to Dart - Intro to Classes and Objects - Part Three

dart-bird.jpg

Repository

https://github.com/dart-lang/sdk

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

Requirements

System Requirements:
OS Support for Dart:
  • Windows
  • macOS
  • Linux

Required Knowledge

  • 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:

Sources:

Dart Logo (Google): https://www.dartlang.org/

Difficulty

  • Beginner

Description

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.

construtor.png

In this image, we have the primary constructor at the top. This constructor initializes both the real and imaginary values of the Complex object. This is important, because both of these values must contain some value to create a true Complex Number. The named constructors, Complex.real and Complex.imaginary both make use of the initializer operation. They use this operation to call the primary constructor with a specific set of rules. Complex.real populates the imaginary field with a zero and Complex.imaginary populates 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.


override.png

In this image, an example is shown where we override both the toString() and == 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 print function. In this case, we check to see if the imaginary field is negative. If its positive, we print the complex number with a 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 aComplex 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

Video Tutorial

Curriculum

H2
H3
H4
Upload from PC
Video gallery
3 columns
2 columns
1 column
7 Comments