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 Abstract Classes


Share

Dart Abstract Classes

An abstract class is a class that you cannot create objects from.

It is used as a superclass for other classes.

You create an abstract class with the abstract keyword before class:

Example

abstract class Animal {
  String name;

  Animal(this.name);

  void sleep() {
    print('$name is sleeping');
  }
}

class Cat extends Animal {
  Cat(super.name);
}

void main() {
  var cat = Cat('Tom');
  cat.sleep();
}
Try it Yourself »

Result:

Tom is sleeping

You Cannot Create an Object

If you try to create an object from an abstract class, you get an error:

Example

abstract class Animal {
  void sleep() {
    print('Sleeping');
  }
}

void main() {
  var animal = Animal();
  animal.sleep();
}
Try it Yourself »

Result:

main.dart:8:16: Error: The class 'Animal' is abstract and can't be instantiated.
  var animal = Animal();
               ^^^^^^

This is the point of an abstract class.

A general idea like "an animal" or "a shape" is not a real thing on its own.

Only the subclasses, like Cat or Circle, are real things.



Abstract Methods

An abstract method is a method without a body.

You write the method name and its parameters, and end it with a semicolon ; instead of curly brackets.

A normal class cannot have abstract methods, only an abstract class can.

Every subclass must override all abstract methods:

Example

abstract class Shape {
  double area();
}

class Circle extends Shape {
  double radius;

  Circle(this.radius);

  @override
  double area() => 3.14 * radius * radius;
}

class Square extends Shape {
  double side;

  Square(this.side);

  @override
  double area() => side * side;
}

void main() {
  print(Circle(2).area());
  print(Square(3).area());
}
Try it Yourself »

Result:

12.56
9.0

If a subclass does not override an abstract method, you get an error:

Example

abstract class Shape {
  double area();
}

class Triangle extends Shape {
  double base = 4;
  double height = 3;
}

void main() {
  print(Triangle().base);
}
Try it Yourself »

Result:

main.dart:5:7: Error: The non-abstract class 'Triangle' is missing implementations for these members:
 - Shape.area
Try to either
 - provide an implementation,
 - inherit an implementation from a superclass or mixin,
 - mark the class as abstract, or
 - provide a 'noSuchMethod' implementation.
class Triangle extends Shape {
      ^^^^^^^^
main.dart:2:10: Context: 'Shape.area' is defined here.
  double area();
         ^^^^

Abstract and Normal Methods Together

An abstract class can have both abstract methods and normal methods.

A normal method can even call an abstract method.

Each subclass gives its own version of the abstract method:

Example

abstract class Employee {
  String name;

  Employee(this.name);

  double salary();

  void printPay() {
    print('$name earns \$${salary()} per month');
  }
}

class Manager extends Employee {
  Manager(super.name);

  @override
  double salary() => 5000;
}

class Intern extends Employee {
  Intern(super.name);

  @override
  double salary() => 1200;
}

void main() {
  Manager('Sara').printPay();
  Intern('Ben').printPay();
}
Try it Yourself »

Result:

Sara earns $5000.0 per month
Ben earns $1200.0 per month

A List of Different Objects

You can use the abstract class as a type.

A list of type List<Shape> can hold circles, squares and any other shape:

Example

abstract class Shape {
  String get name;
  double area();
}

class Circle extends Shape {
  double radius;
  Circle(this.radius);

  @override
  String get name => 'Circle';

  @override
  double area() => 3.14 * radius * radius;
}

class Square extends Shape {
  double side;
  Square(this.side);

  @override
  String get name => 'Square';

  @override
  double area() => side * side;
}

void main() {
  List<Shape> shapes = [Circle(1), Square(2), Circle(2)];
  for (var shape in shapes) {
    print('${shape.name}: ${shape.area()}');
  }
}
Try it Yourself »

Result:

Circle: 3.14
Square: 4.0
Circle: 12.56

As you can see, a getter can also be abstract.

Remember: Use an abstract class when several classes share the same idea, but each class does some of the work in its own way.



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.