A Beginners Guide to Dart - Inheritance, Abstract Classes, Interfaces, Mixins and Casting - Part Five

in #utopian-io5 years ago

dart-bird.jpg

Repository

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

What Will I Learn?

  • You will learn about Abstract Classes
  • You will learn about Class Inheritance
  • You will learn about Class Interfaces
  • You will learn about Type Casting
  • You will learn about Mixins

Requirements

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

Required Knowledge

  • The Dart SDK
  • A Dart supported text editor (Dart Pad can be used)
  • 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 video tutorial, we continue to look at Object Oriented and Class based Programming in the Dart programming language. To fully understand why Classes and Objects are useful in a programming context, we need to understand inheritance and the Dart single inheritance model. This includes looking at Abstract Classes, Class Interfaces, Mixins and Type Casting. Along the way, we also look at importing libraries and the logical switch statement.

Abstract Classes and Interfaces

An abstract class is a type of class that doesn't allow for object instantiation. This tool exists so that developers may further generalize their class blueprints into interfaces. As such, abstract classes are used to define methods and properties without including implementation details. These methods, properties and other details make up the interface of the Class. Dart makes use of these class interfaces to create a description of the complex relationships between classes. This enables Inheritance, Implementations and Mixins despite the language only allowing a single inheritance model.


inheritance-shapes.png

In the example, we take a look at direct inheritance using an abstract Shape class. The Shape class allows us to define the interface for any other Shape objects in our program. To be a Shape type, an object must implement three getter methods; a name method, a area method and a perimeter method. This graph shows the flow of inheritance between the three main classes that we created in the example. Circle and Rectangle directly inherit from Shape and therefore must have their own implementations of the Shape getters. Square on the other hand inherits from Rectangle which allows us to save writing a new implementation of the Shape Interface. While Square's direct parent class is the Rectangle Class, it still is a Shape type because both implement the Shape interface.

Adding Properties and Methods Without Using Inheritance

Dart's single inheritance model may seem restrictive when compared to multiple inheritance models from other programming languages. Dart provides us with several ways to include attributes from multiple other classes outside of inheritance. Every Dart class automatically exposes its interface and a class can implement multiple interfaces which allows us to build composite classes.


class-c.png

In this image, we have an example of a complex class. This C class implements the interfaces for class A and class B by way of the implements keyword. C must contain the full interface for both A and B to satisfy this call. C also adds a third class called TimeStamp through the use of a Mixin. For a class to be used as a Mixin, it must follow three specific rules. The class must not have a declared constructor, it also can not be a subclass to another class and it must not have any calls to the super keyword. When the class fits these rules, its behavior can be mixed directly into another class using the with keyword.

The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-4

Video Tutorial

Curriculum

Sort:  

Hello @tensor

Again, another excellent video tutorial added to this series.

I like the fact that when you teach, you stay very focused on each concept that is being taught. This is the best way for students to grasp new concepts.

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]

As always @rosatravels, thanks for moderating my contribution.

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!

Congratulations @tensor! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 5000 as payout for your posts. Your next target is to reach a total payout of 6000

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Support SteemitBoard's project! Vote for its witness and get one more award!

Hi, @tensor!

You just got a 6.05% 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.

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

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!

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57296.76
ETH 3055.75
USDT 1.00
SBD 2.32