Control how your code is used. Real OOP toolkit — problem first, solution second.
Press J anytime to jump to any section. Arrow keys to navigate.
Quick reminder of everything you already know.
init, add, commit, status
branches, review, merge
team project in groups
blueprint — properties + methods
default + named (this.x shortcut)
child class reuses parent
📌 Today: more class tools. Problem → Solution for each.
Each one controls who can use or extend your class.
Force children to fill in methods
Children must extend (not implement)
Children only implement (no inheritance)
No children at all
Only these exact children allowed
Smart way to create objects (bonus)
📌 We'll do each one with 3 slides: Why → How → Real Use.
Like a form template that says "you MUST fill in these fields." The template itself is useless — you need to fill it.
📌 We need: force every payment type to have its OWN pay() code.
Declare methods with no body.
Create Payment() directly.
Children MUST write pay().
Like a recipe book. Chefs can add new recipes (extend). But no one can photocopy it and claim it's theirs (implement).
📌 Sometimes we want: "Children must use MY setup."
extends Account — allowed.
implements Account — blocked.
Child also must be base, final, or sealed.
Like a job description. "You must do task A and task B." But HOW you do them is up to you.
📌 Sometimes we want: "just the shape, no inherited code."
extends Printable — blocked.
implements Printable — required.
Children rewrite ALL methods.
Like a sealed box. No one can open it, extend it, or copy its shape. Use as-is.
📌 Sometimes we want: "no copies, no children, just this."
extends — blocked.
implements — blocked.
Use for value/ID classes.
Like a fixed menu. Only these 3 items exist — Momo, Thukpa, Dal Bhat. No one can add "Pizza" secretly.
📌 Sometimes we want: "Only these 3 subclasses. No more."
Fixed children — same file only.
Works great with switch.
Compiler checks ALL cases.
📌 Miss a case? Compiler error. Zero bugs.
abstract class means?sealed gives you?final class blocks?Like a drink machine. You press "cola" — the machine picks the right can. You don't pick cans directly.
📌 Sometimes we want: "pick which class based on input."
Return any subtype.
Return cached object.
Uses return — no this.
factory can?thisreturn keywordnewLike a USB skill stick. Plug the same skill into many devices. Camera, phone, laptop all get it.
📌 We want: write once, use in many classes.
Many classes use the same mixin.
Class can use many mixins.
No extends — uses with.
📌 Order class gets both log() and printReceipt() for free.
on — Constrain Where Mixin UsedMixin needs a field from parent? E.g. balance?
📌 on Account = "only classes extending Account can use me."
Like a labeled box. Box says "Savings" and inside is the rate (5%). Label + data together.
📌 We want: data lives INSIDE the enum.
Fields stored with value.
Methods allowed.
Must use const constructor.
Like a sticker. Stick a new button on an existing TV remote. Can't open the remote, but you added a feature.
📌 We want: add method to built-in class without editing it.
Add methods to ANY class.
Even String, int, List.
Use this to refer to the object.
Everything from today — abstract, factory, sealed, mixins, extensions. Click each feature.
with keyword is used for?Force implementation
Only extend
Only implement
No children
Fixed family
Smart creation
Share skills (with, on)
Data + methods
Add methods anywhere
Need force methods?
Fixed list of types?
Lock class down?
Share code across classes?
Fixed values with data?
Add method to String/int?
Khalti and IME to sealed Payment
Loggable on all payments
OrderStatus with icon field
int for .rupees
factory Payment.of(String)
Use branches! Create feature/your-name and submit a PR.
Before next class: finish your Payment System assignment and push to GitHub.
Practice at dartpad.dev — try rebuilding the Payment System from scratch!