Constructors in Dart

Learn via video courses
Topics Covered

Overview

Constructors in Dart are fundamental methods that both create and initialize objects within classes, allowing you to define the object's creation process and set its initial state. Dart supports two main types of constructors: default constructors, which are automatically generated if not explicitly defined, and named constructors, providing custom labels for alternative object initialization approaches. These constructors can accept parameters for initializing object properties and can also call other constructors using the this keyword. Constructors are essential in Dart programming, serving as the cornerstone for efficient and organized object instantiation.

Syntax

In Dart, a constructor is a special member function of a class that is responsible for initializing the newly created objects of that class. It is used to set initial values to the object's properties and perform any necessary setup. Here's the syntax and explanation of creating a constructor in Dart:

Syntax:

Explanation:

  • class ClassName: This is where you declare your class, and ClassName is the name of the class you're defining.
  • ClassName(parameters): This is the constructor declaration. The constructor name should be the same as the class name (ClassName in this case). You can also include parameters within the parentheses. Parameters are values that you can pass to the constructor when you create an object of the class.
  • { // Constructor body }: This is the constructor's body, enclosed within curly braces. Inside the constructor body, you can write code that initializes the object's properties and performs any necessary setup. This code will be executed whenever you create an instance of the class using this constructor.

In this example:

  • The main function is the entry point of the program. It creates an instance of the Student class by calling its constructor and passing the values Jones and 26 as arguments.

  • The Student class has a constructor named Student. This constructor takes two parameters: a string str (for the student's name) and an integer age (for the student's age). Inside the constructor, the code prints out the provided name and age.

  • When the Student constructor is called with the values Jones and 26, it automatically prints:

    Output:

Types of Constructors in Dart

  • Default Constructor A default constructor is the one that doesn't take any parameters. If you don't explicitly define a constructor for a class, Dart provides a default constructor automatically. It initializes the instance variables with their default values (e.g., null for objects, 0 for integers, false for booleans).

    Default constructors are handy for creating objects without explicitly specifying constructor calls. However, developers can also define custom constructors with different parameter sets to suit various object creation scenarios. When using default constructors, Dart provides a concise way to initialize object properties during instantiation.

    Example:

    Output:

  • Parameterized Constructor A parameterized constructor in Dart is a custom constructor defined within a class that allows you to initialize the object's properties using parameters passed during object creation. Unlike the default constructor, which is generated automatically, parameterized constructors provide more flexibility by allowing you to customize object initialization based on specific requirements. Parameterized constructors enable you to create objects with different initial values for properties, which is especially useful when you need to create objects with specific attributes or configurations. By defining custom constructors, you can tailor object creation to your application's needs.

    Example:

    Output:

  • Named Constructor In Dart, a named constructor is a custom constructor within a class that is given a specific name, allowing you to create objects using different initialization logic or parameter sets apart from the default constructor. Named constructors provide a way to add multiple ways of creating objects with varying configurations.

    Named constructors are beneficial when you want to provide alternative ways of constructing objects, which can be particularly useful for handling different input formats, conversions, or special initialization scenarios. They allow for more expressive and meaningful object creation in your Dart code.

    Example:

    Output:

  • Factory Constructor

    In Dart, a factory constructor is a special type of constructor used to create objects in a different manner than a typical constructor. It can return an instance of the class it belongs to, or even an instance of a subclass, by providing custom logic for object creation. Factory constructors are particularly useful when the process of creating an object involves complex calculations, caching, or condition-based decision-making.

    Factory constructors can be advantageous for encapsulating complex initialization logic or for managing object creation scenarios where caching or instance reuse is required. They offer a flexible way to construct objects and allow you to control the object creation process more effectively.

    Example:

    Output:

Conclusion

  • Constructors in Dart are used to initialize objects and set their initial values.
  • Dart provides a default constructor if one is not defined explicitly.
  • Multiple constructors can be defined using names to create objects with different configurations.
  • Constructors can take parameters to initialize object properties during creation.
  • Initializer lists allow setting instance variables before the constructor body runs.
  • Factory constructors return instances of a class, allowing custom object creation logic.
  • Constructors can call other constructors in the same class using named constructors.
  • If a constructor doesn't explicitly call a superclass constructor, the default superclass constructor is implicitly called.