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 Inheritance


Share

Dart Inheritance

Inheritance means that one class gets the fields and methods of another class.

  • Superclass (or parent class): the class that is inherited from
  • Subclass (or child class): the class that inherits

To inherit from a class, use the extends keyword.

In this example, the Dog class inherits from the Animal class:

Example

class Animal {
  String name = 'Animal';

  void eat() {
    print('$name is eating');
  }
}

class Dog extends Animal {
  void bark() {
    print('Woof!');
  }
}

void main() {
  var dog = Dog();
  dog.name = 'Rex';
  dog.eat();
  dog.bark();
}
Try it Yourself »

Result:

Rex is eating
Woof!

The Dog class has no name field and no eat() method of its own.

It gets them from Animal.

Note: A Dart class can only extend one class.


Override a Method

A subclass can write its own version of a method from the superclass.

This is called overriding the method.

Put @override above the method to show that it replaces a method from the superclass:

Example

class Animal {
  void makeSound() {
    print('The animal makes a sound');
  }
}

class Cat extends Animal {
  @override
  void makeSound() {
    print('Meow');
  }
}

class Cow extends Animal {
  @override
  void makeSound() {
    print('Moo');
  }
}

void main() {
  Animal().makeSound();
  Cat().makeSound();
  Cow().makeSound();
}
Try it Yourself »

Result:

The animal makes a sound
Meow
Moo

@override is an annotation, a note for Dart and for other programmers.

The code also runs without it, but it is good practice to always write it.

The tools can then warn you if the method name is wrong and nothing is overridden.



Call a Superclass Method

The super keyword refers to the superclass.

Use super.methodName() to call the superclass version of a method from inside the subclass.

This lets you add to a method instead of replacing it completely:

Example

class Vehicle {
  void start() {
    print('Engine started');
  }
}

class Car extends Vehicle {
  @override
  void start() {
    super.start();
    print('Car is ready to drive');
  }
}

void main() {
  var car = Car();
  car.start();
}
Try it Yourself »

Result:

Engine started
Car is ready to drive

Call the Superclass Constructor

If the superclass has a constructor with parameters, the subclass must call it.

You do this with : super(...) after the parameter list of the subclass constructor:

Example

class Person {
  String name;

  Person(this.name);
}

class Student extends Person {
  String school;

  Student(String name, this.school) : super(name);
}

void main() {
  var student = Student('Emma', 'Oak High');
  print(student.name);
  print(student.school);
}
Try it Yourself »

Result:

Emma
Oak High

If you forget to call the superclass constructor, you get an error:

Example

class Person {
  String name;

  Person(this.name);
}

class Student extends Person {
  String school;

  Student(this.school);
}

void main() {
  var student = Student('Oak High');
  print(student.school);
}
Try it Yourself »

Result:

main.dart:10:3: Error: The implicitly called unnamed constructor from 'Person' has required parameters.
Try adding an explicit super initializer with the required arguments.
  Student(this.school);
  ^

Super Parameters

Dart has a shorter way to pass a parameter to the superclass constructor.

Write super.name in the parameter list, in the same way as this.name.

This is called a super parameter:

Example

class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void introduce() {
    print('Hi, I am $name and I am $age years old.');
  }
}

class Teacher extends Person {
  String subject;

  Teacher(super.name, super.age, this.subject);

  @override
  void introduce() {
    super.introduce();
    print('I teach $subject.');
  }
}

void main() {
  var teacher = Teacher('Olivia', 41, 'Math');
  teacher.introduce();
}
Try it Yourself »

Result:

Hi, I am Olivia and I am 41 years old.
I teach Math.

Teacher(super.name, super.age, this.subject) does the same as Teacher(String name, int age, this.subject) : super(name, age).


Check the Type

An object of a subclass is also an object of its superclass.

A Dog is an Animal, so you can store it in a variable of type Animal:

Example

class Animal {
  void makeSound() {
    print('Some sound');
  }
}

class Dog extends Animal {
  @override
  void makeSound() {
    print('Woof');
  }
}

void main() {
  Animal pet = Dog();
  pet.makeSound();
  print(pet is Dog);
  print(pet is Animal);
}
Try it Yourself »

Result:

Woof
true
true

The variable has the type Animal, but Dart runs the Dog version of the method, because the object is a Dog.



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.