10.9 C
New York
Wednesday, February 28, 2024

Inheritance in Dart: A Quick Example

In Dart, you can use the extends keyword with any class where you want to extend the superclass’s functionality. A class can only extend one class since Dart does not support multiple inheritances.

Below is a quick example of implementing inheritance in Dart. Imagine we are building an employee management project of a company. The most general class will be Employee and other classes will inherit it:Advertisements

import 'package:flutter/foundation.dart';

// Define the Employee class
class Employee {
  final String first;
  final String last;

  Employee(this.first, this.last);

  String toString() {
    return '$first $last';

// Define the subclass which extends the Employee class
class Developer extends Employee {
  final String _role;

  Developer(this._role, String first, String last) : super(first, last);

  String toString() {
    return '${super.toString()}: $_role';

void main() {
  final _dev = Developer('Flutter Developer', 'John', 'Doe');

The preceding code will produce the following output:

John Doe: Flutter Developer

Further reading:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.


Related Articles

Latest Articles