Classes in Dart

Part 1 of 2

Dart is a language that is defined as being Object Oriented, but what does this mean exactly. In this article we will learn about Object Oriented Programming, what it is and how it is incorporated into Dart. We will also see how to create and use classes, and how to manage data within classes using properties and methods.

What is Object Oriented Programming?

In very simple terms, Object Oriented Programming, or OOP, is a way of programming that is based on the idea of objects.

Creating Classes

Classes are created using the class keyword, followed by the name of the class. For example, we can create the Person class as follows personclass.png

In any class we also could have what we have known up until this point as functions, in Object Oriented Programming however we call them methods.

An example of how a method would look like in a class is;

functions.png

Constructors

A constructor is

  • a method that is called when an instance of a class is created

  • named after the class

  • has the same name as the class

  • can take parameters

  • can only be called once per instance

Constructors might not be necessary. In Dart, a default constructor is provided if you do not create one. This constructor has no arguments and does nothing.

Types of constructors

Named constructor

Named constructors are used to

  • implement multiple constructors for a class

  • implement a single constructor with multiple purposes.

Here is an example;

namedconstructor.png

In this example, we have a class named Rectangle which has two fields: width and height. The class also has two constructors. The first one is the default constructor which takes two parameters: width and height. The second constructor is named square.

The named constructor is

  • declared using the class name followed by a dot and the name of the constructor.

In this example, we want to be able to create a square Rectangle object.

We can do this by using the square constructor.

When this constructor is called, it sets the width and height fields to the same value.

Getters and Setters

  • get the value of a field

  • set the value of a field

The general syntax for a getter is

get fieldName {}
  • Gets are declared using the get keyword

  • The fieldName is the name of the field that the getter is associated with

  • The body of the getter can be empty or it can contain code

  • If the getter has a body, the body must contain a return statement

A getter does not take any arguments. Take a look at the following example.

class Person {

String _name;

Person(this._name);

String get name {

return _name.toUpperCase();

}

}

void

main() {

final person = Person('Bob');

print(person.name);

}

The output of this program is

BOB

In this example, we have a class named Person. This class has one field named _name which is marked as private. The class also has a constructor which takes one parameter: _name. The constructor sets the _name field to the value of

the parameter.

The getter for this class is named name. When this getter is called, it returns the value of _name converted to uppercase.

Getters and setters can be used to validate data.

The general syntax for a setter is

set fieldName(value) {}

  • Setters are declared using the set keyword

  • The fieldName is the name of the field that the setter is associated with

  • value is a parameter that will hold the value that is being assigned to the field.

  • The body of the setter can be empty or it can contain code.

  • If the setter has a body, the body must contain a return statement.

An example of a setter is

class Person {

String

_name;

Person(this._name);

set name(String newName) {

_name = newName.toUpperCase();

}

}

void

main() {

final person = Person('Bob');

person.name = 'Alice';

print(person._name);

}

The output of this program is

ALICE

In this example, we have a class named Person. This class has one field named _name which is marked as private. The class also has a constructor which takes one parameter: _name. The constructor sets the _name field to the value of the parameter.

The setter for this class is named name. When this setter is called, it sets

_name to the value of the newName parameter converted to uppercase.

Conclusion

This brings us to the end of the first article in this series. See you in the next one