šŸ—ļø Lesson 5: Object-Oriented Basics

ā± 35-45 min šŸ“Š Intermediate šŸ—ļø Project-Based šŸ”„ Core Concept 2026 šŸ”— Official Source
šŸ—ļø

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
šŸ“ name: String
šŸ”¢ age: int
🐾 species: String
šŸ—ļø 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%

Loading question...