Menu
Coddy logo textTech

Constructors & Inheritance

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

When a child class extends a parent, the parent's constructor must be called to properly initialize inherited fields. Dart provides flexible ways to handle this through the initializer list using super.

If the parent has a parameterized constructor, the child must call it explicitly:

class Person {
  String name;
  int age;
  
  Person(this.name, this.age);
}

class Student extends Person {
  String school;
  
  Student(String name, int age, this.school) : super(name, age);
}

Dart 2.17 introduced super parameters, a cleaner syntax that forwards parameters directly to the parent constructor:

class Student extends Person {
  String school;
  
  // super.name and super.age forward directly to Person's constructor
  Student(super.name, super.age, this.school);
}

When working with named constructors, you specify which parent constructor to call:

class Employee extends Person {
  String department;
  
  Employee(super.name, super.age, this.department);
  
  Employee.intern(String name, this.department) : super(name, 18);
}

The key rule is simple: the parent must be fully initialized before the child adds its own properties. Whether you use the traditional super() call or the newer super parameters, Dart ensures the inheritance chain is properly constructed from parent to child.

challenge icon

Challenge

Easy

Let's build a product inventory system that demonstrates how constructors work with inheritance. You'll create a hierarchy where different product types extend a base product class, each requiring proper initialization through parent constructors.

You'll organize your code into two files:

  • product.dart: Define your product hierarchy here:
    • A Product class with String name and double price properties, a constructor that takes both values, and a method info() that prints [name]: $[price]
    • A Book class that extends Product and adds a String author property. Use super parameters to forward name and price to the parent constructor
    • A Electronics class that extends Product and adds an int warranty property (in months). Use the traditional super() call in the initializer list to pass values to the parent
  • main.dart: Import your product file and demonstrate both constructor inheritance approaches:
    • Create a Book with name 'Dart Essentials', price 29.99, and author 'Jane Smith'
    • Call its info() method, then print Author: [author]
    • Print an empty line
    • Create an Electronics with name 'Wireless Mouse', price 45.50, and warranty 24
    • Call its info() method, then print Warranty: [warranty] months

Both child classes inherit the info() method from Product without needing to override it. The key difference is in how they call the parent constructor - Book uses the cleaner super parameters syntax, while Electronics uses the traditional approach with super() in the initializer list.

Expected output:

Dart Essentials: $29.99
Author: Jane Smith

Wireless Mouse: $45.5
Warranty: 24 months

Cheat sheet

When a child class extends a parent, the parent's constructor must be called to properly initialize inherited fields.

Traditional approach - explicitly call parent constructor in initializer list:

class Person {
  String name;
  int age;
  
  Person(this.name, this.age);
}

class Student extends Person {
  String school;
  
  Student(String name, int age, this.school) : super(name, age);
}

Super parameters (Dart 2.17+) - forward parameters directly to parent constructor:

class Student extends Person {
  String school;
  
  Student(super.name, super.age, this.school);
}

Named constructors - specify which parent constructor to call:

class Employee extends Person {
  String department;
  
  Employee(super.name, super.age, this.department);
  
  Employee.intern(String name, this.department) : super(name, 18);
}

Try it yourself

import 'product.dart';

void main() {
  // TODO: Create a Book with:
  // - name: 'Dart Essentials'
  // - price: 29.99
  // - author: 'Jane Smith'

  // TODO: Call the book's info() method

  // TODO: Print "Author: [author]"

  // TODO: Print an empty line

  // TODO: Create an Electronics with:
  // - name: 'Wireless Mouse'
  // - price: 45.50
  // - warranty: 24

  // TODO: Call the electronics' info() method

  // TODO: Print "Warranty: [warranty] months"
}
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