A Beginners Guide to Dart - Intro to Classes and Objects - Part Three
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:
- IDEA intellij or Visual Studio Code with the Dart Plugins
- The Dart SDK
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:
- 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
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.
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.
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
Leave A Beginners Guide to Dart - Intro to Classes and Objects - Part Three to:
Read more #utopian-io posts
Best Posts From Tensor Programming
We have not curated any of tensor's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Tensor Programming
- Introduction to Elixir - Recursion, Tail Calls and Comprehensions - Part Five
- Introduction to Elixir - Guards and Conditional Macros - Part Four
- Introduction to Elixir - Pattern Matching and Control Flow - Part Three
- Introduction to Elixir - Functions, Built-in and Complex Types - Part Two
- Introduction to Elixir - A Background and the Primitive Types - Part One
- Phoenix Framework - Building a Chat Room with Phoenix Live View and Server Side Rendering
- A Beginners Guide to Dart - The Tooling Ecosystem - Part Nine
- A Beginners Guide to Dart - Concurrency, Asynchronous Evaluation and Parallelism in Dart - Part Eight
- Using Flare to Build Vector Graphics and Animations in Flutter
- Building a Flutter Speech to Text/Speech Recognizer Application
- A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types - Part Seven
- Dart Project - Building a Client Side Tetris Game - Part Two
- Dart Project - Building a Client Side Tetris Game - Part One
- A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six
- Working With gRPC in Flutter on Top of a Go Service
- A Beginners Guide to Dart - Inheritance, Abstract Classes, Interfaces, Mixins and Casting - Part Five
- A Beginners Guide to Dart - Methods, Final, Static, and Class Inheritance - Part Four
- Building a Chat app with Docker and gRPC
- A Beginners Guide to Dart - Intro to Classes and Objects - Part Three
- Building an Basic API with gRPC and Protobuf in Go