Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 1

Why OOP exists

The problem procedural code cannot solve

12m30 XP
→→→

What you'll be able to do

  • Describe what goes wrong when data and behaviour live apart
  • Explain procedural programming in one sentence — and its limits
  • Say what OOP actually reorganises (it is not 'more code')
  • Recognise when objects are the right tool and when they are overkill
1

Why OOP Exists

Programs model things, and things have both data and behaviour

Every program is a model of something real: students, accounts, orders, cars. The question is only *how* you organise that model.

Look at a real bank account. It has data (a number, an owner, an id) and behaviour (deposit, withdraw, print a statement). Those two things belong together: the rule *'you may not withdraw more than the balance'* is meaningless without the balance, and the balance is dangerous without the rule. Object-Oriented Programming is simply the decision to keep the data and the rules that protect it inside the same unit — an object. Everything else in this course (encapsulation, inheritance, polymorphism, interfaces) is a technique for doing that well.

The ATM analogy

You never reach into a bank's database and edit your balance. You use an ATM: a small set of buttons (deposit, withdraw, check balance) that protect the money behind them. The ATM is an object: data inside, buttons outside, rules enforced in the middle. Procedural code, by contrast, is like leaving the vault door open and asking every employee to remember the rules.

How a procedural program decays as it grows

  1. Day 1: a couple of variables and three methods. Everything is clear.
  2. Day 10: you need 200 students, so variables become parallel arrays: names[], ages[], grades[].
  3. Day 20: you add emails[]. Now every method that walks students must be updated, and one wrong index silently pairs Sara's name with Omar's grade.
  4. Day 30: the rule 'grade must be 0..100' exists in five different methods — and one of them forgot it.
  5. Day 40: nobody can change anything safely, because the data has no owner.

OOP is about ownership

When you ask 'who is allowed to change this value, and under which rules?' and the answer is a single class — you are doing OOP correctly. When the answer is 'anyone, anywhere', no amount of syntax will save the project.

2

Procedural vs OOP

Procedural vs object-oriented, side by side

Same feature, two designs

Procedural

Data in arrays, rules in loose methods

Main.java
1String[] names = { "Sara", "Omar" };
2double[] balances = { 500, 300 };
3
4static void deposit(double[] b, int i, double amount) {
5 b[i] = b[i] + amount; // who checks amount > 0?
6}
  • The name and the balance are only connected by an index
  • Any code anywhere can write balances[0] = -999
  • Adding a field means touching every method
  • The validation rule has no home

Object-oriented

Data and rules in one unit

Main.java
1class Account {
2 private String owner;
3 private double balance;
4
5 public void deposit(double amount) {
6 if (amount > 0) {
7 balance = balance + amount;
8 }
9 }
10}
  • One object = one owner + one balance, permanently paired
  • private means outside code cannot corrupt the balance
  • The rule lives exactly where the data lives
  • Adding a field touches one class

The differences that actually matter

QuestionProceduralOOP
Unit of organisationthe methodthe class
Where does data live?variables / arrays passed aroundinside the object that owns it
Who enforces a rule?every caller, by conventionthe class, by construction
Adding a new kind of thingedit existing if/else chainsadd a new class (see Module 8)
Best forsmall scripts, one-off calculationssystems that grow and change
Feel the problem yourself

This is the procedural version. Add an `emails` array and print it too — notice how many places you must touch, and how easy it is to misalign an index.

Output
Press Run to execute this example.

Common mistakes

WrongThinking OOP means 'writing more classes'.
WhyA class that is only a bag of public fields gives you none of the benefits — it is procedural code wearing a costume.
FixA class should own its data and expose behaviour. If it has no rules, ask why it exists.
WrongBelieving procedural code is always wrong.
WhyA 20-line script that converts a file needs no objects at all.
FixUse objects when things have identity, state and rules that must be protected over time.

Lesson playground

Free practice

Parallel arrays in action. Swap two values in `ages` only and watch the data quietly become wrong — nothing crashes, which is exactly the danger.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

What is the core problem OOP was invented to solve?

Summary

  • Every program models real things, and real things have data *and* rules.
  • Procedural code separates them, so rules get duplicated and data gets corrupted as the program grows.
  • OOP puts state and behaviour in one unit with a single owner.
  • Objects are a maintenance tool, not a performance trick — and small scripts still do fine without them.