Object Oriented Programming and Classes in the Dart Programming Language - Dart Tutorial Part 2

in utopian-io •  3 months ago

dart-logo.png

Repository

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

What Will I Learn?

  • You will learn about Classes and Objects in Dart
  • You will learn about Initializer Lists
  • You will learn about Constructors and Named Constructors
  • You will learn how to use Factory Constructors
  • You will learn about Inheritance and Abstract Classes
  • You will learn about Getters and Setters as well as Properties and Methods
  • You will learn about the Object base Type

Requirements

System Requirements:
OS Support for Dart:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • Some knowledge of basic programming
  • Some understanding classes and objects
  • A fair understanding of development and Imperative or Object Oriented Programming

Resources for Flutter and this Project:

Sources:

Dart Logo: https://www.dartlang.org/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Dart Video Tutorial, we take a look at Dart's brand of Object Oriented Programming. We talk about how to build Classes and make Constructors for those classes. This includes Default Constructors, Named Constructors and Factory Constructors. We also look at getters and setters and normal methods as well as properties. We take a look at Inheritance in Dart; both implements and extends and we also look at abstract classes. Finally, we look at Initialization Lists and some of the basic properties of the Object base class.

Making a Blue Print for an Object

Classes by default are blueprints to allow the developer to instantiate objects. Dart gives us various tools and means to accomplish this in the programming language specification. Classes can have fields and properties, methods, and multiple constructors in Dart and they have their own life-cycle that needs to be understood to fully harness the power of the syntactic sugar that exists in the language.

factory.png

In the code above, we have a factory constructor for the Point class that was created in this video. This factory constructor allows us to create a new Point class when the associated name property does not exist inside of the key/value cache. If the name does exist inside of the cache then we pull that object out of the cache. These types of factory constructors are useful for creating constructors that do not directly instantiate an instance of the class attached to them.

Using Getters and Setters for Private Variables

Dart features shorthand syntax for creating getter and setter methods for classes. The get keyword is used for creating a getter and the set keyword is used for creating a setter. Getters allow us to get the value of a property from an object and setters allow us to change the value of this property. This type of behavior allows the developer to control which fields in the objects can be read and written to.

get-set.png

Here you can see the getters and setters for the Point class. Because the properties for _x and _y are private, we need to use explicit getters and setters to gain access to their values. We also are able to create getters and setters that are based off of the properties that are built into the object like the add getter.

Abstract Classes and Inheritance

Dart features a single inheritance model for its brand of Object Oriented Programming. This means that a class can only have one superclass associated with it. Dart however, gives us tools to work around this restriction such as the implements keyword. We can use abstract classes and implements to define a pattern and interface that can be used to create other classes. This is very useful when it comes to code-reuse and consistent definitions.

abstract.png

In our example here, we create two abstract classes called A and B respectively. Class A contains the fields a and b and Class B contains a single field c. We can't instantiate either class A or class B because they are abstract but we can implement both classes into another class, class C. By doing this, class C takes on the pattern of both A and B combined and we have to override all three of the fields associated with A and B inside of C.

The source code for this project can be found here

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Again as always, thank you for another excellent clear tutorial!

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? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

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

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

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

Vote for Utopian Witness!