Classes & Objects in Dart
In this lesson, you will learn how classes and objects work together in Dart. Classes define the structure of data, and objects are real instances created from those classes.
This lesson focuses on using classes and objects with real data so you can clearly understand how Dart programs behave in real applications.
Recap: Class vs Object
A class is a blueprint. An object is a real instance created from that blueprint.
For example:
- Class: Employee
- Objects: Rahul, Ananya, Suresh
Creating a Class with Real Data
Let’s create a class to represent an Employee in a company. Each employee has a name, role, and monthly salary.
class Employee {
String name = '';
String role = '';
double salary = 0.0;
void showDetails() {
print('Name: $name');
print('Role: $role');
print('Salary: $salary');
}
}
Creating an Object from a Class
Now let’s create an object from the Employee class and assign real values.
void main() {
Employee emp1 = Employee();
emp1.name = 'Rahul';
emp1.role = 'Software Engineer';
emp1.salary = 65000;
emp1.showDetails();
}
Here, emp1 is an object representing one employee.
Multiple Objects from the Same Class
A single class can create multiple objects. Each object holds its own data.
void main() {
Employee emp1 = Employee();
emp1.name = 'Asha';
emp1.role = 'HR Manager';
emp1.salary = 55000;
Employee emp2 = Employee();
emp2.name = 'Vikram';
emp2.role = 'Product Manager';
emp2.salary = 75000;
emp1.showDetails();
emp2.showDetails();
}
Why Classes Make Code Better
Without classes, you would need separate variables for every employee. Classes group related data together.
- Cleaner code
- Less duplication
- Easy to scale
- Easy to maintain
Real-World Example: Product Inventory
Let’s model a product in an online store.
class Product {
String name = '';
double price = 0.0;
int stock = 0;
void showProduct() {
print('Product: $name');
print('Price: $price');
print('Stock: $stock');
}
}
Using the product class:
void main() {
Product p1 = Product();
p1.name = 'Laptop';
p1.price = 75000;
p1.stock = 12;
p1.showProduct();
}
Objects Store Independent Data
Each object maintains its own state. Updating one object does not affect another.
Product p2 = Product();
p2.name = 'Phone';
p2.price = 25000;
p2.stock = 30;
📝 Practice Exercises
Exercise 1
Create a class called Student with properties name, marks, and grade.
Exercise 2
Create two student objects and assign different values.
Exercise 3
Create a method to display student details.
✅ Practice Answers
class Student {
String name = '';
int marks = 0;
String grade = '';
void showStudent() {
print('Name: $name');
print('Marks: $marks');
print('Grade: $grade');
}
}
void main() {
Student s1 = Student();
s1.name = 'Neha';
s1.marks = 88;
s1.grade = 'A';
Student s2 = Student();
s2.name = 'Karan';
s2.marks = 72;
s2.grade = 'B';
s1.showStudent();
s2.showStudent();
}
What’s Next?
In the next lesson, you will learn about Constructors in Dart, which allow you to initialize objects easily and safely at the time of creation.