Dart – 9 – Classes and Objects

Classes and Objects in Dart Programming

Classes and objects are fundamental concepts in Dart, as they allow you to model and organize your code in a structured manner. In this discussion, we’ll explore the significance of classes and objects in Dart, understand how to create and use them, and discover their role in building modular and maintainable code.

Understanding Classes

In Dart, a class is a blueprint for creating objects. It defines the structure and behavior of an object, including its properties (also known as fields or attributes) and methods (functions associated with the object). Classes serve as templates that help you create multiple instances of objects with shared characteristics.

Here’s an example of a simple Dart class:


class Person {
    String name;
    int age;

    void introduceYourself() {
        print('Hello, my name is $name, and I am $age years old.');
    }
}
    
Creating Objects (Instances)

Once you have defined a class, you can create objects, also referred to as instances, based on that class. Each object has its own set of properties and can invoke methods defined in the class.

Here’s how you can create objects from the Person class:


Person person1 = Person();
Person person2 = Person();
    
Accessing Properties and Methods

To access the properties and methods of an object, you use the dot notation. This allows you to set or retrieve the values of the object’s properties and call its methods.

For example, you can set the name and age properties and call the introduceYourself method for person1 and person2:


person1.name = 'Alice';
person1.age = 30;
person1.introduceYourself();

person2.name = 'Bob';
person2.age = 25;
person2.introduceYourself();
    
Constructors in Classes

Dart provides a special method called a constructor to initialize the properties of an object when it is created. Constructors are defined within the class and can have parameters to set the initial values.

Here’s an example of a class with a custom constructor:


class Rectangle {
    double width;
    double height;

    Rectangle(this.width, this.height);
}
    

In this example, the constructor of the Rectangle class takes two parameters to set the initial values of width and height.

You can create a Rectangle object using the constructor like this:


Rectangle myRectangle = Rectangle(10.0, 5.0);
    
Inheritance and Object-Oriented Concepts

Dart supports object-oriented programming (OOP) concepts, including inheritance, which allows you to create new classes based on existing ones. Inheritance promotes code reuse and the creation of specialized classes.

Here’s an example of inheritance in Dart:


class Animal {
    String name;
    void makeSound() {
        // Abstract method, will be overridden in subclasses.
    }
}

class Dog extends Animal {
    @override
    void makeSound() {
        print('$name barks!');
    }
}

class Cat extends Animal {
    @override
    void makeSound() {
        print('$name meows!');
    }
}
    

In this example, Dog and Cat classes inherit from the Animal class and override the makeSound method to provide their own implementations.

Conclusion

Classes and objects are fundamental to Dart programming and facilitate the creation of organized, modular, and maintainable code. By defining classes and creating objects, you can model and manipulate real-world entities, encapsulate data and behavior, and leverage object-oriented concepts like inheritance. These tools enable you to build robust and flexible applications in Dart.