Classes and Objects
The blueprint and the house.
Classes and Objects
Analogy: lego
A class is like a Lego Blueprint. It tells you what pieces you need and how they fit together.
An object is the actual Lego Castle you build from that blueprint. You can build 100 castles from one blueprint!
The Cookie Factory (Class vs Object)
The Blueprint (Class)
Instances (Objects)
No objects created yet.
The Cookie Cutter Analogy
Imagine a cookie cutter.
- The Cutter is the Class. It defines the shape (Star, Circle).
- The Cookies are the Objects. You stamp them out one by one.
Java Example
Switch language in Navbar
class Cookie {
String shape;
String flavor;
void bake() {
System.out.println("Baking " + flavor + " cookie!");
}
}
// Usage
Cookie c1 = new Cookie();
c1.flavor = "Chocolate Chip";
c1.bake();
Up Next
Constructors