OOP Building Blocks (Class, Object, Field, Method)

Context: FIT2099_MOC · the four atoms of object orientation · the blueprint→instance idea · built with constructors, used via the client relationship Parent Framework: Java Program Structure

Quick Revision

  • 🎯 Objective: model a domain thing as a class, then create objects from it ➔ class = blueprint, object = instance.
  • 📦 Core Components: class (template) ➔ object (instance) | field/attribute (data) | method/behaviour (action).
  • ⚡ 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

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