Article image Dart Basics: Classes and Objects

3.6. Dart Basics: Classes and Objects

Page 24 | Listen in audio

3.6 Basic Dart Concepts: Classes and Objects

Dart is an object-oriented language, which means that almost everything is an object, including functions and null. In Dart, classes are the main way to define objects. Understanding classes and objects is critical to mastering Dart and, by extension, Flutter.

Classes

In Dart, a class is a template or blueprint from which objects are created. A class represents a set of properties (variables) and methods (functions) that are common to all objects of a given type.

To define a class in Dart, we use the keyword 'class' followed by the name of the class. For example, if we wanted to create a class to represent a car, we could do something like this:

class Car {
  String model;
  int year;
  
  void drive() {
    print('Vroom Vroom');
  }
}

In this example, 'Car' is a class with two properties, 'model' and 'year', and a method called 'drive'.

Objects

An object is an instance of a class. It has a state and a behavior. An object's state is stored in its properties and behavior is defined by its methods. To create an object from a class, we use the 'new' keyword followed by the class name and parentheses. For example, to create an object of the 'Car' class, we would do the following:

var myCar = new Car();

In this example, 'myCar' is an object of class 'Car'. We can access an object's properties and methods using the '.' operator. For example, to set the model and year of our car and then make the car drive, we would do the following:

myCar.model = 'Tesla Model S';
myCar.year = 2020;
myCar.drive();

Constructors

A constructor is a special method that is called when an object is created. In Dart, every class has a default constructor, which takes no parameters and does nothing. However, you can define your own constructor for a class, which can be useful for initializing an object's properties when it is created.

To define a constructor for a class, we use the name of the class followed by parentheses. Inside the parentheses, we can define the parameters for the constructor. For example, we could define a constructor for the 'Car' class that accepts the model and year as parameters:

class Car {
  String model;
  int year;
  
  Car(String model, int year) {
    this.model = model;
    this.year = year;
  }
  
  void drive() {
    print('Vroom Vroom');
  }
}

Now, when we create a new car, we can pass the model and year directly to the constructor:

var myCar = new Car('Tesla Model S', 2020);

Conclusion

Classes and objects are fundamental concepts in Dart and object-oriented programming in general. Classes allow us to define models for objects, and objects allow us to create instances of those models. Constructors allow us to initialize an object's properties when it is created. Mastering these concepts is essential for building apps with Flutter and Dart.

Now answer the exercise about the content:

What is the function of a constructor in Dart?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Dart Basics: Inheritance and Polymorphism

Next page of the Free Ebook:

25Dart Basics: Inheritance and Polymorphism

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text