OOP Basics in Dart
In this lesson, you will learn the fundamentals of Object-Oriented Programming (OOP) in Dart. OOP helps developers structure programs using real-world concepts like objects, classes, and behaviors.
Dart is a fully object-oriented language, meaning everything in Dart is an object, including numbers, strings, and functions.
What Is Object-Oriented Programming?
Object-Oriented Programming is a programming paradigm based on the concept of objects. Objects represent real-world entities that contain both data and behavior.
For example:
- A Student object can have name, age, and marks
- A BankAccount object can have balance and account number
- A Car object can have speed and fuel level
Why Use OOP?
OOP is widely used in large-scale applications because it:
- Makes code easier to understand
- Improves reusability
- Helps manage complex systems
- Models real-world problems naturally
Most modern software systems—including mobile apps, web services, and enterprise tools— are built using OOP principles.
Core OOP Concepts
There are four core concepts in Object-Oriented Programming:
- Class
- Object
- Encapsulation
- Abstraction
In upcoming lessons, you will explore each of these in detail.
What Is a Class?
A class is a blueprint or template used to create objects. It defines the properties (variables) and behaviors (methods) that objects will have.
Think of a class as a design, and objects as actual instances created from that design.
Creating a Simple Class in Dart
Let’s create a simple class representing a Student.
class Student {
String name = '';
int age = 0;
void displayInfo() {
print('Name: $name');
print('Age: $age');
}
}
This class defines two properties and one method.
What Is an Object?
An object is an instance of a class. Once a class is defined, you can create multiple objects from it.
Each object has its own data but follows the same structure defined by the class.
Creating Objects in Dart
Here is how you create and use an object in Dart:
void main() {
Student s1 = Student();
s1.name = 'Rahul';
s1.age = 20;
s1.displayInfo();
}
Output:
Name: Rahul
Age: 20
Multiple Objects from One Class
You can create multiple objects from the same class. Each object stores its own data.
void main() {
Student s1 = Student();
s1.name = 'Asha';
s1.age = 21;
Student s2 = Student();
s2.name = 'Vikram';
s2.age = 23;
s1.displayInfo();
s2.displayInfo();
}
Real-World Example: Bank Account
Let’s model a simple bank account using OOP concepts.
class BankAccount {
String accountHolder = '';
double balance = 0.0;
void deposit(double amount) {
balance += amount;
}
void showBalance() {
print('Balance: $balance');
}
}
Using the class:
void main() {
BankAccount acc = BankAccount();
acc.accountHolder = 'Suresh';
acc.deposit(5000);
acc.showBalance();
}
Benefits of Using Classes and Objects
- Code becomes modular
- Logic is grouped logically
- Easy to maintain and extend
- Better readability
📝 Practice Exercises
Exercise 1
Create a class called Car with properties brand and speed.
Exercise 2
Create two objects from the Car class and assign different values.
Exercise 3
Add a method to display car details.
✅ Practice Answers
Answer
class Car {
String brand = '';
int speed = 0;
void showDetails() {
print('Brand: $brand');
print('Speed: $speed');
}
}
void main() {
Car c1 = Car();
c1.brand = 'Toyota';
c1.speed = 120;
Car c2 = Car();
c2.brand = 'BMW';
c2.speed = 180;
c1.showDetails();
c2.showDetails();
}
What’s Next?
In the next lesson, you will learn about Classes and Objects in more depth, including constructors and better ways to initialize objects.