Classes and Objects in Dart
1. What is a Class?
A class in Dart is like a blueprint or model for creating objects .
Blueprint is also like a model .
It defines:
- What properties (variables/data) an object will have
- What methods (functions/behavior) the object can do
Example: If a house is an object, then a class is the house plan.
Get MD. SAKIF RAHAMAN MISHAD’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax of a Class in Dart:
class Person {
String name = '';
int age = 0;
void sayHello() {
print("Hello, my name is $name and I am $age years old.");
}
}Here:
- name and age are fields (variables)
- sayHello() is a method
2. What is an Object?
An object is a real-world instance of a class. It’s like building an actual house from the blueprint.
Creating an Object
void main() {
Person p1 = Person(); // creating an object
p1.name = "Sakif";
p1.age = 23;
p1.sayHello(); // Output: Hello, my name is Sakif and I am 23 years old.
}Here:
- p1 is an object of the Person class
- You can access the object’s properties and methods using the dot (.) operator





