Lesson 1: What is OOP? (Why We Need It)
Duration: 15 minutes
App Feature: π― Planning our Expense Manager
What You'll Learn: Understand how OOP helps organize our expense tracking app
Introduction
Welcome to your journey of learning Dart and Object-Oriented Programming! Instead of learning abstract concepts, we're going to build a real expense manager app together. By the end of this tutorial, you'll understand not just what OOP is, but why it's essential for building apps.
The Problem: Life Without OOP
Imagine you want to track your expenses. Let's say you want to record:
- A coffee you bought for $4.50
- Groceries for $85.50
- Netflix subscription for $15.99
Approach 1: Separate Variables (The Bad Way)
void main() {
// First expense - Coffee
String expense1Description = 'Coffee';
double expense1Amount = 4.50;
String expense1Category = 'Food';
DateTime expense1Date = DateTime.now();
// Second expense - Groceries
String expense2Description = 'Groceries';
double expense2Amount = 85.50;
String expense2Category = 'Food';
DateTime expense2Date = DateTime.now();
// Third expense - Netflix
String expense3Description = 'Netflix';
double expense3Amount = 15.99;
String expense3Category = 'Entertainment';
DateTime expense3Date = DateTime.now();
print('Expense 1: $expense1Description - \$$expense1Amount');
print('Expense 2: $expense2Description - \$$expense2Amount');
print('Expense 3: $expense3Description - \$$expense3Amount');
}
Problems with This Approach:
- Messy and repetitive - We need 4 variables for each expense!
- Hard to manage - What if you have 50 expenses? That's 200 variables!
-
No relationship - Nothing connects
expense1Description
withexpense1Amount
-
Error-prone - Easy to mix up
expense1Amount
withexpense2Amount
- Can't reuse code - Want to calculate total? You need to add each variable manually
Try to imagine tracking 10 expenses this way... That's 40 separate variables! π±
The Solution: Object-Oriented Programming
OOP lets us bundle related data together. Instead of 4 separate variables per expense, we create a blueprint (called a class) and make objects from it.
Think of it Like This:
Real World Analogy:
π House Blueprint (Class)
- A blueprint shows what every house should have: rooms, doors, windows
- It's not an actual house, just a template
π‘ Actual Houses (Objects)
- You can build many houses from one blueprint
- Each house is unique (different colors, sizes) but follows the blueprint
In Programming:
π Expense Class (Blueprint)
- Defines what every expense should have: description, amount, category, date
- Just a template, not actual data
π° Expense Objects (Actual Expenses)
- Coffee expense: $4.50, Food, Today
- Netflix expense: $15.99, Entertainment, Today
- Each is a real expense created from the template
Understanding Classes and Objects
What is a Class?
A class is a blueprint or template that defines:
- Properties (data): what information it stores
- Methods (actions): what it can do
Think of it as a cookie cutter - it shapes what the cookies will look like, but it's not the cookie itself.
What is an Object?
An object is an actual instance created from a class.
Think of it as the cookies - real things you can eat, made from the cookie cutter template.
Our First Look at OOP Code
Don't worry if you don't understand everything yet - we'll learn step by step. This is just to show you the difference:
// The BLUEPRINT - This is a class
class Expense {
String description;
double amount;
String category;
DateTime date;
}
void main() {
// Creating OBJECTS from the blueprint
var coffee = Expense();
coffee.description = 'Coffee';
coffee.amount = 4.50;
coffee.category = 'Food';
coffee.date = DateTime.now();
var netflix = Expense();
netflix.description = 'Netflix';
netflix.amount = 15.99;
netflix.category = 'Entertainment';
netflix.date = DateTime.now();
print('${coffee.description}: \$${coffee.amount}');
print('${netflix.description}: \$${netflix.amount}');
}
See the difference?
- We defined the
Expense
structure once - We created multiple expenses easily
- Each expense is a separate object with its own data
- The code is cleaner and more organized
Why OOP Matters for Your Expense Manager
As we build our expense manager app, you'll see how OOP helps us:
1. Organization
Without OOP: 100 expenses = 400+ variables floating around
With OOP: 100 expense objects, each neatly packaged
2. Reusability
// Want to add a new expense? Just create an object!
var lunch = Expense();
var dinner = Expense();
var gas = Expense();
// Same blueprint, different data
3. Maintainability
// Need to add a "notes" field to all expenses?
// Change the class ONCE, all objects get the new feature
class Expense {
String description;
double amount;
String category;
DateTime date;
String notes; // Added here - affects all expenses!
}
4. Real-World Modeling
Your app will have:
- Expense objects (each purchase)
- Category objects (Food, Transport, etc.)
- Budget objects (monthly limits)
- User object (your profile)
Each is a separate class, working together!
The Four Pillars of OOP
As we build our expense manager, we'll learn these four core concepts:
1. Encapsulation π
Bundle data and methods together, hide internal details.
In our app: Keep expense data safe, validate amounts (can't be negative!)
2. Inheritance π³
Create new classes based on existing ones.
In our app: RecurringExpense
(like Netflix) inherits from Expense
but adds frequency
3. Polymorphism π
One action, multiple forms.
In our app: All expenses can printDetails()
, but recurring ones show frequency too
4. Abstraction π―
Hide complex details, show only what's necessary.
In our app: You don't need to know HOW expenses are stored, just add/view/delete them
Don't worry if these seem confusing now! We'll learn each one by actually using it in our app.
What We're Building Together
Over the next lessons, we'll build a complete expense tracking system:
π± Expense Manager App
βββ π° Expense Class
β βββ Track amount, date, category
β βββ Validate data (no negative amounts!)
β βββ Calculate totals
β
βββ π RecurringExpense Class
β βββ Monthly bills (rent, subscriptions)
β βββ Calculate yearly costs
β
βββ π― OneTimeExpense Class
β βββ Special purchases (gifts, emergencies)
β βββ Tag occasions
β
βββ π³ PaymentMethod Classes
β βββ Credit card, cash, digital wallet
β βββ Process payments differently
β
βββ π ExpenseManager Class
βββ Store all expenses
βββ Filter by category
βββ Calculate totals
βββ Generate reports
Quick Check: Do You Understand?
Before moving to the next lesson, make sure you can answer these:
β Self-Check Questions:
- What's the difference between a class and an object? Click to see answer
- Class = Blueprint/template (defines structure)
- Object = Actual instance (real data)
- Example:
Expense
is the class, your coffee purchase is an object
- Why is OOP better than using separate variables? Click to see answer
- Organizes related data together
- Easier to manage many items
- Less error-prone
- Code is reusable
- Name one real-world example of a class and its objects Click to see answer
Examples:
- Class:
Car
β Objects: Your Toyota, Your friend's Honda - Class:
Student
β Objects: John, Sarah, Mike - Class:
Song
β Objects: "Bohemian Rhapsody", "Hotel California"
Try It Yourself! π―
Before moving to Lesson 2, think about what properties an Expense
should have:
Your Task:
Write down (on paper or in notes) what information you'd want to track for each expense:
Example answers:
- β Description (what you bought)
- β Amount (how much it cost)
- β Category (Food, Transport, etc.)
- β Date (when you bought it)
- ? What else would be useful?
Bonus questions to think about:
- Should expenses have a unique ID number?
- Should we track the payment method (cash, card)?
- Should we allow notes or tags?
We'll use your ideas in the next lesson when we actually create the Expense
class!
Key Takeaways π
Before moving forward, remember:
β
OOP organizes code into objects - like real-world things
β
Classes are blueprints - they define structure
β
Objects are instances - actual data created from classes
β
OOP makes code cleaner - easier to read and maintain
β
Perfect for apps - Flutter itself is built on OOP principles
What's Next?
In Lesson 2, we'll:
- Write our first
Expense
class - Create actual expense objects
- Learn about constructors
- Add methods to print expense details
- See our code actually work!
Time to get hands-on and write real code! π
Need Help?
Common questions beginners ask:
Q: Do I need to memorize all the OOP terms?
A: No! Focus on understanding the concepts. The terms will become natural as you use them.
Q: Is OOP hard?
A: It might feel strange at first, but by building a real app, you'll see why it's actually easier than the alternative!
Q: Can I skip OOP and just learn Flutter?
A: Flutter IS built with OOP! Every widget is a class. Understanding OOP will make Flutter much easier.
Q: What if I get stuck?
A: That's normal! Re-read sections, try the examples, and remember - we're building this step by step together.
Ready for Lesson 2? Let's write some code! β
Top comments (0)