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

in #utopian-io6 years ago

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

Sort:  

Hi @tensor

These beginners guides to Dart is going to be so helpful to people who want to learn about all the different features of building classes / objects.

I like it that you refer people to do extra resource of the website.

Also I didn't know there are video tutorials of this on you - tube as I never searched for this.

Your video tutorials are informative and easy to follow because they flow easily from one concepts to another.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you as always @rosatravels. Yeah, I think this series will come out quite well by the end as a comprehensive series that people can follow no matter their background.

Thank you for your review, @rosatravels! Keep up the good work!





This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hi, @tensor!

You just got a 5.33% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!