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:
- IDEA intellij, Visual Studio Code with the Dart/Flutter Plugins, Android Studio or Xcode
- The Dart SDK on the latest developer build for Dart 2 support.
- An Android or iOS Emulator or device for testing
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:
- Dart Website: https://www.dartlang.org/
- Dart Awesome GitHub Repository: https://github.com/yissachar/awesome-dart
- Dartpad Tool: https://dartpad.dartlang.org/
- Dart SDK repository: https://github.com/dart-lang/sdk
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.
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.
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.
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