Creating a Non-Instantiable, Non-Extendable Class Creating a Non-Instantiable, Non-Extendable Class dart dart

Creating a Non-Instantiable, Non-Extendable Class


Giving you class a private constructor will make it so it can only be created within the same file, otherwise it will not be visible. This also prevents users from extending or mixing-in the class in other files. Note that within the same file, you will still be able to extend it since you can still access the constructor. Also, users will always be able to implement your class, since all classes define an implicit interface.

class Foo {  /// Private constructor, can only be invoked inside of this file (library).  Foo._();}// Same file (library).class Fizz extends Foo {  Fizz() : super._();}// Different file (library).class Bar extends Foo {} // Error: Foo does not have a zero-argument constructorclass Fizz extends Object with Foo {} // Error: The class Foo cannot be used as a mixin.// Always allowed.class Boo implements Foo {}