What is a Class in Dart?

A class in Dart is a blueprint for creating objects. It defines the properties (attributes) and methods (functions) that the objects created from the class will have. Classes are fundamental to object-oriented programming (OOP) and allow for encapsulation, inheritance, and polymorphism.

1. Defining a Class

To define a class in Dart, you use the class keyword followed by the class name. By convention, class names in Dart use PascalCase.

Example of a Class Definition

class Car {
// Properties
String color;
String model;

// Constructor
Car(this.color, this.model);

// Method
void displayInfo() {
print('Car model: $model, Color: $color');
}
}

In this example, we define a class named Car with two properties: color and model. We also define a constructor that initializes these properties and a method displayInfo that prints the car's information.

2. Creating Objects from a Class

Once a class is defined, you can create objects (instances) of that class using the new keyword or simply by calling the class constructor.

Example of Creating Objects

void main() {
// Creating an object of the Car class
Car myCar = Car('Red', 'Toyota');

// Calling the method on the object
myCar.displayInfo(); // Output: Car model: Toyota, Color: Red
}

In this example, we create an instance of the Car class named myCar and call the displayInfo method to print its details.

3. Constructors

Constructors are special methods that are called when an object is created. Dart provides a default constructor if no constructors are defined. You can also define named constructors for more specific initialization.

Example of Named Constructor

class Car {
String color;
String model;

// Default constructor
Car(this.color, this.model);

// Named constructor
Car.fromColor(String color) : this.color = color {
this.model = 'Unknown'; // Default model
}

void displayInfo() {
print('Car model: $model, Color: $color');
}
}

void main() {
Car myCar = Car.fromColor('Blue');
myCar.displayInfo(); // Output: Car model: Unknown, Color: Blue
}

4. Inheritance

Dart supports inheritance, allowing a class to inherit properties and methods from another class. This promotes code reuse and establishes a relationship between classes.

Example of Inheritance

class Vehicle {
void start() {
print('Vehicle started');
}
}

class Car extends Vehicle {
String color;
String model;

Car(this.color, this.model);

void displayInfo() {
print('Car model: $model, Color: $color');
}
}

void main() {
Car myCar = Car('Red', 'Toyota');
myCar.start(); // Output: Vehicle started
myCar.displayInfo(); // Output: Car model: Toyota, Color: Red
}

In this example, the Car class inherits from the Vehicle class, allowing it to use the start method defined in the Vehicle class.

5. Conclusion

Classes are a fundamental concept in Dart that enable object-oriented programming. They provide a way to encapsulate data and behavior, allowing for the creation of complex data structures and promoting code reuse through inheritance. Understanding how to define and use classes is essential for building robust and maintainable Dart applications.