⚡ Key Constraint: a class is the design; an object is a concrete thing built from it via new — one class, many objects each with their own field values.
📝 How It Works
1. Class vs Object
Class ➔ a blueprint/template describing how objects of that type look; the coffee-machine design.
Object ➔ an instance created from the class (new CoffeeMachine()); an actual coffee machine off the factory line.
Relationship ➔ one class → many objects, each holding its own data.
2. Fields (Attributes)
Field/attribute ➔ a variable belonging to a class/object — a piece of data (brand, water).
Object references as fields ➔ a field may itself be an instance of another class (MilkContainer milkContainer).
3. Methods (Behaviours)
Method/behaviour ➔ an action an object can perform — update or return its data (serveCoffee()).
⚙️ Core Implementation
🔹 CoffeeMachine
classDiagram + Java
classDiagram
class CoffeeMachine {
-boolean isBrewing
-float water
-String brand
-MilkContainer milkContainer
+serveCoffee() void
}
CoffeeMachine --> MilkContainer : has-a
public class CoffeeMachine { // class = blueprint private String brand; // field / attribute public void serveCoffee() {} // method / behaviour}// Main.javaCoffeeMachine nespresso = new CoffeeMachine(); // object = instance
💡 Common Mistake:- = private, + = public in a classDiagram ➔ fields are usually private (hidden), methods public (the interface) — the basis of information hiding.
When It Flips: the class file name must match the class name (Java Program Structure); each object gets its own copy of the fields, so two CoffeeMachines can hold different brand values while sharing the one serveCoffee() behaviour.
🧠 Active Recall
Distinguish class, object, field, and method with the coffee-machine example.
Answer
Short answer: Class = the CoffeeMachine blueprint; object = a specific new CoffeeMachine() instance; field/attribute = its data (brand, water); method/behaviour = an action (serveCoffee()).
Why:Blueprint vs instance ➔ the class defines structure once; each object instantiates it with its own field values.
Why can a class's field be another class type (e.g. MilkContainer)?
Answer
Short answer: Fields hold data of any type, including references to other objects, so a CoffeeMachine can have-aMilkContainer as an attribute.
Why:Composition ➔ objects are built from other objects; this “has-a” link is the seed of the client–supplier relationship.