Part 3 of 7 · 5-minute read. My learning notes on the main programming paradigms, with Dart examples.
Concept
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.
Real-Life Analogy
Think of a car manufacturing plant. Each car is an object with:
- Properties: color, model, speed, fuel level
- Methods: accelerate(), brake(), refuel()
- Encapsulation: The driver doesn't need to know how the engine works internally
- Inheritance: A SportsCar inherits features from Car but adds turbo boost
- Polymorphism: Both Car and Motorcycle can move(), but they do it differently
Dart Example
// 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 Diagram
╔═══════════════════════════════════════════════════════════════╗
║ 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 Core Principles
╔═══════════════════════════════════════════════════════════════╗
║ 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.
Further Reading
Object-Oriented Programming:
- Object-Oriented Programming in Dart - Official documentation
- Design Patterns - Gang of Four patterns explained
- SOLID Principles - OOP best practices
Dart OOP Features:
- Mixins in Dart - Code reuse technique
- Extension Methods - Extending existing classes
Books:
- Head First Design Patterns - Beginner-friendly approach
- Clean Code - Robert C. Martin
Originally published at projekt.github.io/articles. I'm Srđan Ljuština — I build small, privacy-friendly apps at srdapp.rs. Find me on LinkedIn and GitHub.
Top comments (0)