Classes and Objects in Dart

Understanding classes and objects is fundamental in Dart as it is an object-oriented programming language. This documentation provides an in-depth exploration of classes and objects, including their syntax, usage, and practical examples.

What is a Class?

A class is a blueprint for creating objects. It encapsulates data (attributes) and behavior (methods) that the objects created from the class will have.

Defining a Class

In Dart, you define a class using the class keyword followed by the class name. Class names typically start with an uppercase letter.

Syntax

class ClassName {
  // Properties (fields)   
  Type propertyName;

  // Constructor   
  ClassName(this.propertyName);

  // Method   
  void methodName() {
    // Method body   }
}

Example

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

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

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

Explanation

  • Properties: color, model, and year are attributes that define the state of the Car class.
  • Constructor: The constructor Car(this.color, this.model, this.year); initializes the properties when a new object is created.
  • Method: displayInfo() is a method that prints the car's details.

What is an Object?

An object is an instance of a class. When you create an object, you are using the class as a blueprint. Each object can hold different values for its properties but retains the same structure and behavior defined by its class.

Creating an Object

You create an object using the class constructor.

Syntax

ClassName objectName = ClassName(arguments);

Example

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

  // Calling the method to display information   
  myCar.displayInfo(); // Output: Car Model: Toyota, Color: Red, Year: 2020 
  }

Explanation

  • Car myCar = Car('Red', 'Toyota', 2020);: This line creates a new instance of the Car class named myCar, initializing its properties with the provided values.
  • myCar.displayInfo();: This calls the displayInfo() method on the myCar object, which prints the car's details.

Real-Life Analogy

Think of a class as a blueprint for a house. The blueprint defines what the house will look like (rooms, doors, windows) but is not a physical house itself. An object, on the other hand, is a physical house built from that blueprint. Each house (object) can have different colors, sizes, or furniture, but they all follow the same blueprint (class).

Example in Real Life

  1. Class Definition: Imagine you have a class called Dog. This class can have properties like breed, age, and color, and methods like bark() and fetch().
class Dog {
  String breed;
  int age;
  String color;

  Dog(this.breed, this.age, this.color);

  void bark() {
    print('Woof! Woof!');
  }

  void fetch(String item) {
    print('Fetching the $item!');
  }
}
  1. Creating Objects: Now, you can create different dog objects based on this class.
void main() {
  Dog myDog = Dog('Golden Retriever', 3, 'Golden');
  Dog neighborDog = Dog('Bulldog', 5, 'Brindle');

  myDog.bark(); // Output: Woof! Woof!   
  myDog.fetch('ball'); // Output: Fetching the ball!   
  print('My dog is a ${myDog.breed} and is ${myDog.age} years old.'); 
  // Output: My dog is a Golden Retriever and is 3 years old. }

Explanation of the Example

  • Dog Class: Defines the properties and methods for dog objects.
  • Creating Objects: myDog and neighborDog are instances of the Dog class, each with their own set of attributes.
  • Method Calls: Methods like bark() and fetch() demonstrate behaviors specific to the dog objects.

PLAY QUIZ

What is a class in Dart?

A single instance of an object.

A blueprint for creating objects that encapsulates data and behavior.

A collection of functions.

A way to store multiple values.