Class & Object

  • Steps
    
    
                    // Define a class representing a Car
                    class Car {
                      String brand;
                      String model;
                      int year;
    
                      // Constructor
                      Car(this.brand, this.model, this.year);
    
                      // Method to describe the car
                      void carDetails() {
                        print('Car Details: $year $brand $model');
                      }
                    }
    
                    void main() {
                      // Create objects of the Car class
                      Car myCar = Car('Toyota', 'Corolla', 2022);
                      Car anotherCar = Car('Ford', 'Mustang', 2023);
    
                      // Accessing properties and methods of objects
                      print('My car is a ${myCar.year} ${myCar.brand} ${myCar.model}');
                      myCar.carDetails();
    
                      print('Another car is a ${anotherCar.year} ${anotherCar.brand} ${anotherCar.model}');
                      anotherCar.carDetails();
                    }