šļø Lesson 5: Object-Oriented Basics
Why Object-Oriented Programming?
Imagine building with LEGO blocks. Each block is a self-contained "object" with its own properties and behaviors. OOP lets you build complex programs the same way!
Class = Blueprint
A plan that describes how to build something
Object = Actual House
A real instance built from the blueprint
Properties = Features
Color, size, number of rooms
Methods = Actions
Open door, turn on lights, heat water
Today's Project: "Zoo Management System"
We'll build a complete zoo management application:
- š¾ Animal class hierarchy (inheritance)
- šļø Different animal types (Mammal, Bird, Reptile)
- šµ Each animal makes its own sound (polymorphism)
- š Zoo statistics and reporting
- š§ Professional class design patterns
Step 1
Anatomy of a Class
š Interactive Class Dissector
Animal
Properties
š name: String
š¢ age: int
š¾ species: String
Methods
šļø Animal(name, age, species)
š makeSound()
š get description
š½ļø eat(food)
š Click on any property or method to learn more!
āļø Live Class Builder
š Generated Class Code
class Animal {
String name;
int age;
String species;
Animal(this.name, this.age, this.species);
void makeSound() {
print('$name makes a sound');
}
void eat(String food) {
print('$name eats $food');
}
void sleep() {
print('$name is sleeping');
}
}
Step 2
Constructors: Building Objects
šļø Constructor Types Visualizer
Default Constructor
class Animal {
String name;
int age;
// Default constructor
Animal(this.name, this.age);
}
// Usage
var cat = Animal('Whiskers', 3);
Named Constructor
class Animal {
String name;
int age;
Animal(this.name, this.age);
// Named constructor
Animal.newborn(this.name) : age = 0;
Animal.unknown()
: name = 'Unknown',
age = 0;
}
Factory Constructor
class Animal {
String name;
int age;
static final Map<String, Animal> _cache = {};
Animal._internal(this.name, this.age);
factory Animal(String name, int age) {
return _cache.putIfAbsent(
name,
() => Animal._internal(name, age)
);
}
}
Factory constructors can return cached instances!
Constant Constructor
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
// Compile-time constants
const origin = Point(0, 0);
const unit = Point(1, 1);
Constant constructors create compile-time constants!
Step 3
Inheritance: Building Hierarchies
š³ Interactive Inheritance Tree
š¾ Animal
š Mammal
𦮠Dog
š± Cat
š¦ Bird
š¦ Parrot
š¦
Eagle
š Class Details
Click on any class to see its details!
š Before vs After Inheritance
ā Without Inheritance
class Dog {
String name;
int age;
void eat() { ... }
void sleep() { ... }
void bark() { ... }
}
class Cat {
String name;
int age;
void eat() { ... }
void sleep() { ... }
void meow() { ... }
}
š° Duplicated code everywhere!
ā With Inheritance
class Animal {
String name;
int age;
void eat() { ... }
void sleep() { ... }
}
class Dog extends Animal {
void bark() { ... }
}
class Cat extends Animal {
void meow() { ... }
}
š Clean & reusable!
Step 4
Polymorphism: Many Forms
š Polymorphism in Action
Create Animals:
Make All Animals Speak:
Add animals and make them speak!
š¦ Your Zoo:
No animals yet...
Step 5
Abstract Classes & Interfaces
š Abstract Class Contract
Abstract Class (Contract)
abstract class Shape {
double get area;
double get perimeter;
void display() {
print('Area: $area');
print('Perimeter: $perimeter');
}
}
Implementations
šµ Circle
class Circle extends Shape {
double radius;
Circle(this.radius);
double get area => pi * radius * radius;
double get perimeter => 2 * pi * radius;
}
š¦ Rectangle
class Rectangle extends Shape {
double width, height;
Rectangle(this.width, this.height);
double get area => width * height;
double get perimeter => 2 * (width + height);
}
š
Build: Zoo Management System
š¾ Base Animal Class
šļø Zoo Management Class
š® Main Program
š¤ Program Output Preview
Edit the code to see the output!
š® OOP Challenge: Concept Master Quiz
š Score: 0/5
ā” Streak: 0
ā± Time: 0s
šÆ Accuracy: 100%