⏱️ 5-Minute Read
| Series Navigation: ← Imperative Programming | Article 3 of 7 | Next: Functional Programming → |
About This Series
This article series represents my journey of learning and applying fundamental programming concepts to the Dart programming language. These are my detailed notes and insights from the learning process, which I hope will be valuable to others exploring similar topics.
Object-Oriented Programming organizes code around objects that bundle data (properties) and behavior (methods) together. It’s based on concepts like encapsulation, inheritance, and polymorphism.
Think of a car manufacturing plant. Each car is an object with:
// Base class
class Vehicle {
String brand;
int speed;
Vehicle(this.brand, this.speed);
void move() {
print('$brand is moving at $speed km/h');
}
void stop() {
speed = 0;
print('$brand has stopped');
}
}
// Inheritance
class Car extends Vehicle {
int numberOfDoors;
Car(String brand, int speed, this.numberOfDoors) : super(brand, speed);
// Method overriding (Polymorphism)
@override
void move() {
print('$brand car with $numberOfDoors doors is driving at $speed km/h');
}
void honk() {
print('Beep beep!');
}
}
class Motorcycle extends Vehicle {
bool hasSidecar;
Motorcycle(String brand, int speed, this.hasSidecar) : super(brand, speed);
@override
void move() {
String sidecarInfo = hasSidecar ? 'with sidecar' : 'without sidecar';
print('$brand motorcycle $sidecarInfo is riding at $speed km/h');
}
}
void main() {
Car myCar = Car('Toyota', 80, 4);
Motorcycle myBike = Motorcycle('Harley', 100, false);
myCar.move(); // Toyota car with 4 doors is driving at 80 km/h
myCar.honk(); // Beep beep!
myBike.move(); // Harley motorcycle without sidecar is riding at 100 km/h
// Polymorphism in action
List<Vehicle> vehicles = [myCar, myBike];
for (var vehicle in vehicles) {
vehicle.move(); // Each calls its own version of move()
}
}
╔═══════════════════════════════════════════════════════════════╗
║ OOP CLASS HIERARCHY ║
╚═══════════════════════════════════════════════════════════════╝
┌─────────────────────────┐
│ Vehicle │
│ (Parent/Base Class) │
╞═════════════════════════╡
│ PROPERTIES: │
│ • brand: String │
│ • speed: int │
╞═════════════════════════╡
│ METHODS: │
│ • move(): void │
│ • stop(): void │
└───────────┬─────────────┘
│
┌───────────┴───────────┐
│ INHERITANCE │
│ (extends) │
└───────────┬───────────┘
│
┌───────────────┴───────────────┐
│ │
┌───────▼──────────┐ ┌────────▼─────────┐
│ Car │ │ Motorcycle │
│ (Child Class) │ │ (Child Class) │
╞══════════════════╡ ╞══════════════════╡
│ PROPERTIES: │ │ PROPERTIES: │
│ • numberOfDoors │ │ • hasSidecar │
╞══════════════════╡ ╞══════════════════╡
│ METHODS: │ │ METHODS: │
│ • move() │ │ • move() │
│ • honk() │ └──────────────────┘
└──────────────────┘
= Polymorphism: Same method name, different implementation
╔═══════════════════════════════════════════════════════════════╗
║ OOP FOUR PILLARS ║
╚═══════════════════════════════════════════════════════════════╝
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 1. ENCAPSULATION ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━┛
┌─────────────────────┐
│ ┌─────────────┐ │ Bundle data and methods
│ │ Private │ │ Hide internal details
│ │ Data │ │ Show only what's needed
│ └──────┬──────┘ │
│ │ │
│ ┌──────▼──────┐ │
│ │ Public │ │
│ │ Methods │ │ ← External access point
│ └─────────────┘ │
└─────────────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 2. INHERITANCE ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━┛
Parent Class
↓ (DNA passed down)
├──► Child 1 (inherits properties)
│
└──► Child 2 (inherits properties)
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 3. POLYMORPHISM ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━┛
Same Interface → Different Implementations
vehicle.move()
├─► Car.move() → "Driving..."
└─► Motorcycle.move() → "Riding..."
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 4. ABSTRACTION ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━┛
User sees: drive()
↓ (Simple interface)
Hidden: Engine
Transmission
Fuel injection
Internal mechanics
Object-Oriented Programming excels at modeling complex systems with many interrelated components. By organizing code around objects that represent real-world entities, OOP makes large codebases more maintainable, reusable, and easier to understand.
Object-Oriented Programming:
Dart OOP Features:
Books:
Srđan Ljuština - Software Developer & Technical Writer
| 🌐 Website | 💻 GitHub |
| Series Navigation: ← Imperative Programming | Article 3 of 7 | Next: Functional Programming → |
Other Articles in This Series: