Java OOP · Module 1
Why OOP exists
The problem procedural code cannot solve
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
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
- Day 1: a couple of variables and three methods. Everything is clear.
- Day 10: you need 200 students, so variables become parallel arrays: names[], ages[], grades[].
- 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.
- Day 30: the rule 'grade must be 0..100' exists in five different methods — and one of them forgot it.
- 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.
Procedural vs OOP
Procedural vs object-oriented, side by side
Same feature, two designs
Procedural
Data in arrays, rules in loose methods
- 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
- One object = one owner + one balance, permanently paired
privatemeans 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
| Question | Procedural | OOP |
|---|---|---|
| Unit of organisation | the method | the class |
| Where does data live? | variables / arrays passed around | inside the object that owns it |
| Who enforces a rule? | every caller, by convention | the class, by construction |
| Adding a new kind of thing | edit existing if/else chains | add a new class (see Module 8) |
| Best for | small scripts, one-off calculations | systems that grow and change |
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.
Common mistakes
Thinking OOP means 'writing more classes'.Believing procedural code is always wrong.Lesson playground
Parallel arrays in action. Swap two values in `ages` only and watch the data quietly become wrong — nothing crashes, which is exactly the danger.
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.