Menu
Coddy logo textTech

Default Constructor

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 10 of 110.

When you don't define any constructor in a class, Dart automatically provides a default constructor. This is a constructor with no parameters that simply creates an instance of the class.

class Book {
  String title = 'Unknown';
  int pages = 0;
}

// Dart provides a default constructor: Book()
Book myBook = Book();
print(myBook.title); // Unknown

The default constructor is invisible in your code, but it exists. It takes no arguments and initializes the object with whatever default values you've assigned to the instance variables.

Once you define your own constructor, the default constructor is no longer provided automatically. If you still want a no-argument constructor alongside a parameterized one, you must explicitly create it:

class Book {
  String title;
  int pages;

  // Explicit default constructor
  Book() : title = 'Unknown', pages = 0;

  // Parameterized constructor
  Book.withDetails(this.title, this.pages);
}

Book book1 = Book();                      // Uses default
Book book2 = Book.withDetails('Dart', 300); // Uses parameterized

The explicit default constructor uses an initializer list (the part after the colon) to set initial values. This pattern is useful when you want to offer multiple ways to create objects from the same class.

challenge icon

Challenge

Easy

Let's build a simple product inventory system that demonstrates both implicit and explicit default constructors.

You'll create two files to organize your code:

  • product.dart: Define a Product class that offers two ways to create products:
    • An explicit default constructor Product() that initializes name to 'Unnamed' and price to 0.0 using an initializer list
    • A named constructor Product.withDetails that accepts name and price parameters
    • A display() method that prints: Product: [name] - $[price]
  • main.dart: Import your product class and create two products:
    • One using the default constructor
    • One using the named constructor with 'Laptop' and 999.99
    Call display() on each product.

Your Product class should have two instance variables:

  • String name
  • double price

Expected output format:

Product: Unnamed - $0.0
Product: Laptop - $999.99

Cheat sheet

When no constructor is defined in a class, Dart automatically provides a default constructor with no parameters:

class Book {
  String title = 'Unknown';
  int pages = 0;
}

Book myBook = Book(); // Uses implicit default constructor
print(myBook.title); // Unknown

Once you define your own constructor, the default constructor is no longer provided automatically. To have both a no-argument constructor and a parameterized one, you must explicitly create them:

class Book {
  String title;
  int pages;

  // Explicit default constructor with initializer list
  Book() : title = 'Unknown', pages = 0;

  // Named constructor
  Book.withDetails(this.title, this.pages);
}

Book book1 = Book();                      // Uses default
Book book2 = Book.withDetails('Dart', 300); // Uses named constructor

The explicit default constructor uses an initializer list (after the colon) to set initial values.

Try it yourself

import 'product.dart';

void main() {
  // TODO: Create a product using the default constructor
  
  // TODO: Create a product using the named constructor with 'Laptop' and 999.99
  
  // TODO: Call display() on each product
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming