Skip to main content

Understanding Classes, Objects, and Instance Variables in Dart

Published 3 min readMarkdown
Part of seriesDart

When learning Dart, understanding classes, objects, and instance variables is essential. These concepts form the foundation of Object-Oriented Programming (OOP), making your code modular, reusable, and organized.

In this post, we’ll break down these concepts with simple examples so you can start using them confidently. 🚀


What is a Class?

A class in Dart is like a blueprint for creating objects. It defines the properties (variables) and behaviors (functions) that the objects created from it will have.

Example:

 Car {
  // Properties (Instance Variables)
  String brand;
  String model;
  int year;

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

  // Method (Behavior)
  void displayInfo() {
    print("This car is a $year $brand $model.");
  }
}

Here:

  • brand, model, and year are instance variables that define the state of the class.

  • The displayInfo method is a behavior of the Car class.


What is an Object?

An object is an instance of a class. It’s like building a specific car using the blueprint. Each object can have unique values for its instance variables.

Example:

void main() {
  // Creating objects of the Car class
  Car car1 = Car("Toyota", "Corolla", 2020);
  Car car2 = Car("Honda", "Civic", 2022);

  // Using the displayInfo method
  car1.displayInfo(); // Output: This car is a 2020 Toyota Corolla.
  car2.displayInfo(); // Output: This car is a 2022 Honda Civic.
}

Here:

  • car1 and car2 are objects created from the Car class.

  • Each object has its own values for brand, model, and year.


What are Instance Variables?

Instance variables are variables declared inside a class but outside any methods. Each object of the class has its own copy of these variables.

Key Points:

  1. Instance variables store data specific to an object.

  2. They are defined at the class level.

  3. They are accessed using the object.


Example with Instance Variables:

class Student {
  // Instance variables
  String name;
  int age;

  // Constructor
  Student(this.name, this.age);

  // Method
  void introduce() {
    print("Hi, my name is $name and I am $age years old.");
  }
}

void main() {
  // Creating objects of Student class
  Student student1 = Student("Alice", 20);
  Student student2 = Student("Bob", 22);

  // Accessing instance variables
  print(student1.name); // Output: Alice
  print(student2.age);  // Output: 22

  // Calling methods
  student1.introduce(); // Output: Hi, my name is Alice and I am 20 years old.
  student2.introduce(); // Output: Hi, my name is Bob and I am 22 years old.
}

Key Takeaways

  • Class: A blueprint that defines properties and behaviors.

  • Object: An instance of a class with its own data.

  • Instance Variables: Variables defined in a class that hold data specific to each object.


Practice Challenge

Write a Dart program for a Book class. The class should have:

  1. Instance variables: title, author, price.

  2. A constructor to initialize these variables.

  3. A method to display book details.

Create two Book objects and display their details. 💪


By understanding these basics, you’re now ready to explore more advanced concepts like inheritance, polymorphism, and abstraction in Dart. Keep coding, and happy learning! 😊

No comments yet. Be the first to comment.

More from this blog