Object-oriented programming (OOP) models program concepts as objects. An object keeps data together with behavior that operates on that data. A class describes shared structure and behavior; each object is an instance created from that template.
🏠 Blueprint → housesOne blueprint creates houses with different addresses and colors.
🍪 Cutter → cookiesThe cutter defines a shape, while every cookie is independent.
📝 Account form → accountsFields and rules are shared; every account has its own owner and balance.
Classes, objects, attributes, and methods
Attributes describe what an object has; methods describe what it can do. Objects share method code, while this directs that code to the current object.
This lesson uses one class per file, with interfaces in separate files too. Match each filename to its type name. Keep files from the same example in one directory, and use separate directories for different examples to avoid duplicate class names.
BankAccount.java
public class BankAccount {
String owner;
String account;
double balance;
void deposit(double amount) {
this.balance += amount;
}
void withdraw(double amount) {
this.balance -= amount;
}
}Main.java
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.owner = "Alice";
account.account = "6250941006528599";
account.balance = 50;
account.deposit(100);
System.out.println(account.balance);
}
}Account object factory: one blueprint, independent state
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Encapsulation and valid state
If arbitrary outside code can make a balance negative, the account enters an invalid state. Encapsulation is not secrecy; it funnels changes through operations such as deposit() and withdraw(), where rules can be checked. private fields reduce accidental outside access by allowing direct access only inside their declaring class. This is a design mechanism, not security encryption.
The constructor
A constructor runs automatically for a new object. It gathers required values at construction time so every instance begins with complete, valid state.
BankAccount.java
public class BankAccount {
private final String owner;
private final String account;
private double balance;
BankAccount(String owner, String account, double balance) {
this.owner = owner;
this.account = account;
this.balance = balance;
}
double getBalance() {
return balance;
}
boolean deposit(double amount) {
if (amount <= 0) {
return false;
}
balance += amount;
return true;
}
boolean withdraw(double amount) {
if (amount <= 0 || amount > balance) {
return false;
}
balance -= amount;
return true;
}
}Constructor and private state: four steps to a valid object
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Create three independent student objects
- Define Student with name and score constructor parameters.
- Implement add_score(points).
- Create three objects with different scores, add 5 to only one, then print all scores.
Build a safe bank account
- Store balance as a private field.
- Let deposit accept only positive values.
- Reject non-positive withdrawals and overdrafts.
- Perform two valid operations and one failed operation; print results and balance.
Encapsulate a thermometer
- Thermometer stores Celsius in a private field.
- set_celsius rejects values below -273.15.
- Provide get_celsius() and get_fahrenheit().
- Test one valid and one invalid value.