Professional Internet Addict • Game Enthusiast • Tech Creator
Professional Internet Addict • Game Enthusiast • Tech Creator

How to call the super class' constructor in Dart language

Here is a quick guide on how to call on a super class' constructor in the Dart language!
Home Blog How to call the super class' constructor in Dart language

Please note that this blog post was published on January 2014, so depending on when you read it, certain parts might be out of date. Unfortunately, I can't always keep these posts fully up to date to ensure the information remains accurate.

    Even though (at the time this article was written at least) it is not explicitly documented here is how to call on a super class' constructor in the Dart language.
    So given we have this abstract class:
    abstract class Animal { String name; Animal( String this.name ); }
    If you are only interested in calling the super's constructor in your sub class' constructor you can write this one liner:
    class Dog extends Animal { Dog() : super("Spot"); }
    Or if you want to do additional logic in the sub class' constructor you can expand the super call like this:
    class Dog extends Animal { Dog() : super("Spot") { print("Dog was created"); } }

    Written by Special Agent Squeaky. First published 2014-01-12. Last updated 2014-01-12.

    📺 Watch Squeaky's latest video!

    How to a Add Simple Real-Time Subtitles to Your Live Stream