DEV Community

Pablo Discobar
Pablo Discobar

Posted on

Quick reminder about dart 3 class modifiers

Shortly explained each modifier in Dart 3

abstract: Abstract classes are those that can't be instantiated directly. They usually contain one or more abstract methods (methods declared without an implementation) which must be implemented by any concrete (i.e., non-abstract) subclass.

base: This modifier ensures that a class or mixin's implementation is inherited. A base class disallows implementation outside of its own library, offering certain guarantees like always calling the base class constructor when a subtype is created.

interface: If a class is declared as an interface, other classes can implement it (i.e., provide their own implementation of all the interface's methods), but they can't inherit from it. This reduces the fragile base class problem, where changes in the base class can break classes that inherit from it.

final: A final class cannot be subclassed. This is useful when you want to prevent other developers from extending your class and potentially misusing it.

sealed: A sealed class is similar to a final class in that it can't be subclassed outside the library it's defined in. However, within the same library, it can be subclassed. This allows for a controlled set of subclasses.

mixin: A mixin is a way of reusing a class's code in multiple class hierarchies.

No modifier: If no modifier is used, the class or mixin can be freely used: it can be instantiated, subclassed, or mixed in.

very simple words with imagine examples :

No modifier: Imagine you have a basic building block, like a Lego brick. You can use it to build anything you want, and there's no restriction on how you can use it. In Dart, a class without a modifier is like that Lego brick. You can use it to create objects, extend it to make new classes, or use it as a blueprint (interface) for other classes.

abstract: This is like a blueprint for a house. It gives you the general idea of what a house should have (like rooms, doors, and windows) but it doesn't exist as a real house that you can live in. You have to build a concrete house using this blueprint. In Dart, an abstract class is like this blueprint. It cannot be used to create objects directly, but other classes can be defined based on it.

base: Think of this as a basic cake recipe that you have to follow exactly as it is. You can add your own toppings or decorations, but you can't change the base recipe. In Dart, a base class is like this cake recipe. Other classes can extend it, but they must use its methods and properties as they are.

interface: This is like a contract for a job. It tells you exactly what tasks you need to perform, but it doesn't care how you do them. In Dart, an interface class is like this contract. It defines a set of methods that must be implemented by any class that uses it.

final: Imagine a sealed toy box. You can play with the toy inside it, but you can't change the toy or add anything to the box. In Dart, a final class is like this sealed toy box. You can create objects from it, but you cannot extend it or change its methods.

sealed: This is like a secret club with a fixed list of members. Only those members can be part of the club, and no one else can join. In Dart, a sealed class is like this secret club. Only a specific set of classes in the same file can extend it.

mixin: This is like an extra skill that you can learn. For example, if you're a chef (class), you could learn a mixin like "baking". This doesn't change the fact that you're a chef, but it adds some new abilities to your skill set. In Dart, a mixin is a way to reuse a class's code in multiple class hierarchies.

Top comments (0)