Menu
Coddy logo textTech

toString() Override

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

When you print an object in Dart, you might see something like Instance of 'Person' - not very helpful. This happens because every class inherits a default toString() method from Object that only returns the class name.

By overriding toString(), you can provide a meaningful string representation of your objects:

class Person {
  String name;
  int age;
  
  Person(this.name, this.age);
  
  @override
  String toString() {
    return 'Person(name: $name, age: $age)';
  }
}

void main() {
  var person = Person('Alice', 30);
  print(person);  // Person(name: Alice, 30)
}

Without the override, print(person) would output Instance of 'Person'. With it, you get useful information about the object's state.

The toString() method is called automatically whenever Dart needs to convert your object to a string - in print() statements, string interpolation, or when concatenating with other strings:

var message = 'User: $person';  // toString() called automatically
print('Found: ' + person.toString());  // Explicit call

A well-implemented toString() makes debugging much easier by letting you quickly inspect object values during development.

challenge icon

Challenge

Easy

Let's build a product catalog system where each product displays its information in a clean, readable format when printed. You'll practice overriding the toString() method to make your objects more debuggable and user-friendly.

You'll organize your code into two files:

  • product.dart: Create a Product class that represents an item in a store. Your product should have a name (String), price (double), and quantity (int). Override the toString() method to return a formatted string in this exact format: Product(name: [name], price: $[price], qty: [quantity])
  • main.dart: Import your product file and demonstrate how the toString() override makes printing objects much more useful. Create three products and print them to see your custom string representation in action:
    • A product named 'Laptop' with price 999.99 and quantity 5
    • A product named 'Mouse' with price 29.5 and quantity 50
    • A product named 'Keyboard' with price 75.0 and quantity 30
    Also demonstrate string interpolation by printing a message that includes one of your products: Featured: [product] using the Laptop product.

Notice how printing each product directly shows meaningful information instead of the unhelpful Instance of 'Product' message. Your toString() method is called automatically both in print() statements and when using string interpolation.

Expected output:

Product(name: Laptop, price: $999.99, qty: 5)
Product(name: Mouse, price: $29.5, qty: 50)
Product(name: Keyboard, price: $75.0, qty: 30)
Featured: Product(name: Laptop, price: $999.99, qty: 5)

Cheat sheet

By default, printing an object in Dart shows Instance of 'ClassName'. Override the toString() method to provide meaningful string representations:

class Person {
  String name;
  int age;
  
  Person(this.name, this.age);
  
  @override
  String toString() {
    return 'Person(name: $name, age: $age)';
  }
}

The toString() method is called automatically in print() statements and string interpolation:

var person = Person('Alice', 30);
print(person);  // Person(name: Alice, age: 30)
var message = 'User: $person';  // toString() called automatically

Try it yourself

import 'product.dart';

void main() {
  // TODO: Create three Product instances:
  // 1. Laptop - price: 999.99, quantity: 5
  // 2. Mouse - price: 29.5, quantity: 50
  // 3. Keyboard - price: 75.0, quantity: 30

  // TODO: Print each product (toString() will be called automatically)

  // TODO: Print a featured message using string interpolation
  // Format: Featured: [laptop 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