Class & Object in Dart
Class:-A class is a blueprint used to create objects. It contains a collection of methods (functions) and properties (attributes) that define the characteristics and behaviors of the object.
OR
A class can be understood as a blueprint or a template used to create objects. It defines a specific data structure that encapsulates data (attributes or properties) and methods (functions or behaviors). A class specifies how the data will be structured and how we can interact with it — although the class itself does not hold any actual data.
About Bulueprint:- A blueprint is a predefined structure or detailed design that defines how an object will be created — what attributes (properties) and methods (behaviors) it will have. It does not hold any real data itself, but it can be used to create multiple real-world instances (objects).
Real Example:-
🚗 Real-Life Analogy: Car Factory & Blueprint
Imagine there’s a Car Factory that wants to manufacture different types of cars.
Before building any actual car, the factory creates a design document — also known as a blueprint. This blueprint defines all the essential characteristics a car should have:
- 4 wheels
- 1 engine
- A color
- A driving mechanism
- A braking system
This blueprint is not a real car — it doesn’t move or take passengers. But it holds the structure and instructions needed to create multiple real cars based on the same design.
You can build:
- A red car
- A blue car
- An electric car
- A sports car
Each of them follows the same blueprint but may differ in actual features or configurations.In object-oriented programming, this blueprint is the class, and the real cars are the objects.
Syntax:-
class_keyword ClassName {
// Constructors
// Properties (fields)
// Methods(Functions)
}Examples:-
// 1. Animal class
class Animal {
// Properties and Methods
}
// 2. Dog class
class Dog {
void bark() {
print("Dog is barking");
}
}
// 3. Student class
class Student {
String name="Raman";
void study() {
print("$name is studying");
}
}
// 4. BankAccount class
class BankAccount {
String accountHolder="Raman";
double balance=10000.00;
void showBalance() {
print("Balance: $balance");
}
}Object:- An object is an instance of a class that combines properties (attributes) and methods (behaviors).
It represents real-world entities — like Student, Car, Mobile, etc.
Get Ajay kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Each object has three key characteristics:
- State:
Represents the current data or condition of the object.
Example: A student’s name = “Raman”, age = 23. - Behavior:
Describes what the object can do or what actions it can perform.
Example: A student canstudy(),attendClass(), etc. - Identity:
Every object has a unique name or reference through which it can be identified.
Example:Student s1 = new Student();— here,s1is the identity of the object.
Instance:- An instance means a “real copy or example of a class.”
Car car = Car(); // car is an Object.Real Example:-
Imagine you have a car design (class) that says — 4 wheels, 1 engine, and a brake system.
Now, when a factory uses that design to build a real car, that real car is called an instance. So, a class is the design, and an instance is the actual object built from that design.
How to create properties in a class
1. Student Class with Single Property
class Student {
String name = "Raman";
}
2. Car Class with Multiple Properties
class Car {
String brand = "Tesla";
int speed = 120;
String color = "Red";
}
3. Light Class with Single Property
class Light {
bool isOn = false;
}
4. Circle Class with Single Property
class Circle {
double radius = 5.0;
}
5. School class Single Property
class School {
List<String> subjects = ["Math", "Science", "English"];
}
6. Country Class with Single Property
class Country {
Map<String, String> states = {
"UP": "Lucknow",
"Bihar": "Patna",
};
}How to create methods in a class
1. Student Class with Single Method
class Student {
void study() {
print("Studying...");
}
}
2. Calculator Class with single Method
class Calculator {
void add(int a, int b) {
print("Sum = ${a + b}");
}
}
3. NumberCheck Class with Single Method
class NumberCheck {
String checkEvenOdd(int n) {
if (n % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
}
4. Marks Class with single method
class Marks {
List<int> getMarks() {
return [90, 85, 78];
}
}
5. Calculator class with multiple methods
class Calculator{
void add(){
print(10+100);
}
void substraction(){
print(20-10);
}
}How to Create an Object and Access properties & Methods from a Class
Meaning of Access:- Access means the ability to use, read, or interact with something.
In programming:
- Accessing a property → means reading or modifying the value of a variable inside a class/object.
- Accessing a method → means calling or invoking the function that belongs to a class/object.
Examples:-
1. Car Class
class Car {
String brand = "Tesla";
int speed = 120;
void showDetails() {
print("Brand: $brand, Speed: $speed km/h");
}
}
void main() {
var car1 = Car(); // create object
print(car1.brand); // access property
print(car1.speed); // access property
car1.showDetails(); // access method
}
2. Student Class
class Student {
String name = "Raman";
int age = 21;
void showDetails() {
print("Name: $name, Age: $age");
}
}
void main() {
var s1 = Student(); // create object
print(s1.name); // access property
print(s1.age); // access property
s1.showDetails(); // access method
}
3. Mobile Class
class Mobile {
String brand = "Samsung";
double price = 25000;
void showDetails() {
print("Brand: $brand, Price: ₹$price");
}
}
void main() {
var m1 = Mobile(); // create object
print(m1.brand); // access property
print(m1.price); // access property
m1.showDetails(); // access method
}
4. Book Class
class Book {
String title = "Flutter Guide";
String author = "John Smith";
void showDetails() {
print("Title: $title, Author: $author");
}
}
void main() {
var b1 = Book(); // create object
print(b1.title); // access property
print(b1.author); // access property
b1.showDetails(); // access method
}Meaning of (.) Dot Operator:-
- The dot (
.) is used to access members of a class (properties and methods) through an object. - It connects the object with its data (variables) and behavior (functions).




