Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Class Modifiers


Share

Dart Class Modifiers

A class modifier is a keyword that you write before class.

It controls what other code can do with your class, for example if it can be extended or implemented.

Dart has these class modifiers:

  • abstract: the class cannot be used to create objects
  • base: the class can be extended, but not implemented
  • interface: the class can be implemented, but not extended
  • final: the class can be neither extended nor implemented
  • sealed: the class has a fixed list of subclasses
  • mixin: the class can also be used as a mixin

You already know abstract classes and sealed classes.

This chapter explains the other modifiers, and gives a summary of all of them at the end.


Modifiers and Libraries

Most class modifiers only limit code in other libraries.

In Dart, every .dart file is a library.

So the rules are about what other files can do with your class, for example when someone uses your package.

Inside the same file, you can still extend an interface class or a final class.

The examples on this page are single files, so they show what works inside one library.

The section Across Two Files shows the errors you get in another library.

Note: You will learn more about libraries and import in the Imports chapter.



base Classes

A base class makes sure that every subtype really inherits its code.

This is useful when the class has important code that must always run, like code that checks or saves data.

There is one rule that works even in the same file.

A class that extends a base class must itself be base, final or sealed.

Without one of these modifiers, the program does not run:

Example

base class Vehicle {
  void move() => print('Moving');
}

class Car extends Vehicle {}

void main() {
  Car().move();
}
Try it Yourself »

Result:

main.dart:5:7: Error: The type 'Car' must be 'base', 'final' or 'sealed' because the supertype 'Vehicle' is 'base'.
Try adding 'base', 'final', or 'sealed' to the type.
class Car extends Vehicle {}
      ^

Add base to the subclass, and it works:

Example

base class Vehicle {
  void move() => print('Moving');
}

base class Car extends Vehicle {}

void main() {
  Car().move();
}
Try it Yourself »

Result:

Moving

This rule makes sure that no class further down can be implemented by other libraries and skip the code of Vehicle.


interface and final Classes

An interface class is meant to be implemented.

Other libraries can implement it with implements, but they cannot extend it with extends.

You can still create objects of an interface class:

Example

interface class Printer {
  void printPage() => print('Printing a page');
}

class LaserPrinter implements Printer {
  @override
  void printPage() => print('Laser printing a page');
}

void main() {
  Printer().printPage();
  LaserPrinter().printPage();
}
Try it Yourself »

Result:

Printing a page
Laser printing a page

A final class cannot be extended or implemented by other libraries at all.

Use it when you want to be sure that nobody else changes how the class works.

Inside the same file, you can still create subclasses, but just like with base, they must be base, final or sealed:

Example

final class Account {
  double balance = 0;

  void deposit(double amount) {
    balance += amount;
  }
}

final class SavingsAccount extends Account {
  double rate = 0.03;
}

void main() {
  var savings = SavingsAccount();
  savings.deposit(100);
  print(savings.balance);
  print(savings.rate);
}
Try it Yourself »

Result:

100.0
0.03

Note: Do not mix up the class modifier final with a final variable.

A final variable cannot get a new value, and a final class cannot be extended or implemented outside its file.


Across Two Files

To see the library rules, you need two files.

That is why this example has no Try it Yourself button.

The first file, shop.dart, has three classes with modifiers:

shop.dart

base class Vehicle {
  void move() => print('Moving');
}

interface class Printer {
  void printPage() => print('Printing');
}

final class Account {
  double balance = 0;
}

The second file, main.dart, imports shop.dart and tries to break each rule:

main.dart

import 'shop.dart';

base class Car implements Vehicle {
  void move() => print('Car moving');
}

class LaserPrinter extends Printer {}

final class Savings extends Account {}

void main() {}

When we ran dart run main.dart, Dart showed these errors:

Result:

main.dart:3:27: Error: The class 'Vehicle' can't be implemented outside of its library because it's a base class.
base class Car implements Vehicle {
                          ^
main.dart:7:28: Error: The class 'Printer' can't be extended outside of its library because it's an interface class.
class LaserPrinter extends Printer {}
                           ^
main.dart:9:29: Error: The class 'Account' can't be extended outside of its library because it's a final class.
final class Savings extends Account {}
                            ^

The same three classes in one file would give none of these errors.


mixin class

In Dart 3, a normal class cannot be used as a mixin:

Example

class Logger {
  void log(String message) => print('LOG: $message');
}

class App with Logger {}

void main() {
  App().log('Started');
}
Try it Yourself »

Result:

main.dart:5:16: Error: The class 'Logger' can't be used as a mixin because it isn't a mixin class nor a mixin.
class App with Logger {}
               ^

A mixin class can be used both as a normal class and as a mixin:

Example

mixin class Logger {
  void log(String message) => print('LOG: $message');
}

class App with Logger {}

void main() {
  App().log('Started');
  Logger().log('Used as a normal class');
}
Try it Yourself »

Result:

LOG: Started
LOG: Used as a normal class

Summary

You can also combine some modifiers.

A common one is abstract interface class, which works like a pure interface in other languages:

Example

abstract interface class Payment {
  void pay(double amount);
}

class CardPayment implements Payment {
  @override
  void pay(double amount) => print('Paid $amount by card');
}

void main() {
  Payment p = CardPayment();
  p.pay(25);
}
Try it Yourself »

Result:

Paid 25.0 by card

This table shows what each modifier allows.

The last three columns are about code in other libraries:

ModifierCreate objectsExtendImplementUse as mixin
class (no modifier)YesYesYesNo
abstract classNoYesYesNo
base classYesYes *NoNo
interface classYesNoYesNo
final classYesNoNoNo
sealed classNoNoNoNo
abstract interface classNoNoYesNo
mixin classYesYesYesYes

* The subclass must be base, final or sealed.

Tip: If you write a small program in one file, you rarely need these modifiers.

They are most useful when you write a library or package that other people use.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.