Menu
Coddy logo textTech

Redirecting Constructors

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

A redirecting constructor delegates to another constructor in the same class. Instead of initializing fields directly, it calls a different constructor to do the work. This helps avoid code duplication when multiple constructors share similar logic.

The syntax uses a colon followed by this and the target constructor:

class Point {
  int x;
  int y;

  // Main constructor
  Point(this.x, this.y);

  // Redirecting constructor - delegates to main constructor
  Point.origin() : this(0, 0);

  // Another redirecting constructor
  Point.onXAxis(int x) : this(x, 0);
}

When you call Point.origin(), it redirects to Point(0, 0), which handles the actual initialization. The redirecting constructor cannot have a body or an initializer list - it simply passes control to another constructor.

Point p1 = Point(3, 5);
Point p2 = Point.origin();
Point p3 = Point.onXAxis(7);

print('p1: (${p1.x}, ${p1.y})'); // p1: (3, 5)
print('p2: (${p2.x}, ${p2.y})'); // p2: (0, 0)
print('p3: (${p3.x}, ${p3.y})'); // p3: (7, 0)

Redirecting constructors are useful when you want to provide convenient shortcuts for common object configurations while keeping all initialization logic in one place. If you later need to change how objects are initialized, you only update the main constructor.

challenge icon

Challenge

Easy

Let's build a time representation system that uses redirecting constructors to provide convenient ways to create time objects.

You'll create two files to organize your code:

  • time_of_day.dart: Define a TimeOfDay class that represents a time with hours and minutes. Your class should have:
    • Two instance variables: int hours and int minutes
    • A main constructor that takes both hours and minutes
    • A redirecting constructor TimeOfDay.midnight() that creates a time at 0:00
    • A redirecting constructor TimeOfDay.noon() that creates a time at 12:00
    • A redirecting constructor TimeOfDay.atHour(int hour) that creates a time at the specified hour with 0 minutes
    • A display() method that prints the time
  • main.dart: Import your time class and create four time objects:
    • A custom time at 9:30 using the main constructor
    • Midnight using the midnight() constructor
    • Noon using the noon() constructor
    • A time at 17:00 using the atHour() constructor
    Call display() on each time in the order listed above.

The display() method should print in this exact format, with hours and minutes zero-padded to two digits:

[HH]:[MM]

For example, 9 hours and 5 minutes should display as 09:05.

Expected output:

09:30
00:00
12:00
17:00

Cheat sheet

A redirecting constructor delegates to another constructor in the same class using this. It helps avoid code duplication by calling a different constructor to handle initialization.

Syntax uses a colon followed by this and the target constructor:

class Point {
  int x;
  int y;

  // Main constructor
  Point(this.x, this.y);

  // Redirecting constructor - delegates to main constructor
  Point.origin() : this(0, 0);

  // Another redirecting constructor
  Point.onXAxis(int x) : this(x, 0);
}

When calling Point.origin(), it redirects to Point(0, 0). Redirecting constructors cannot have a body or initializer list.

Point p1 = Point(3, 5);
Point p2 = Point.origin();
Point p3 = Point.onXAxis(7);

print('p1: (${p1.x}, ${p1.y})'); // p1: (3, 5)
print('p2: (${p2.x}, ${p2.y})'); // p2: (0, 0)
print('p3: (${p3.x}, ${p3.y})'); // p3: (7, 0)

Try it yourself

import 'time_of_day.dart';

void main() {
  // TODO: Create a custom time at 9:30 using the main constructor

  // TODO: Create midnight using the midnight() constructor

  // TODO: Create noon using the noon() constructor

  // TODO: Create a time at 17:00 using the atHour() constructor

  // TODO: Call display() on each time in the order listed above
}
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