These notes recap Topic 3.1. For the full explanations and worked examples, read the study guide.
Recap
- Abstraction reduces complexity by focusing on the main idea and hiding irrelevant detail.
- Data abstraction names data without exposing how it is represented. In a class the data are attributes: variables declared in the class outside any method or constructor.
- Procedural abstraction names a process. A method can be used knowing only what it does, not how.
- Method decomposition breaks a large behaviour into smaller methods, each with one job.
- Parameters generalise a method so it works for a range of arguments.
- You may change a method’s internals without telling its callers if the signature and what it does stay the same.
- Design a class before coding it: list the attributes and the behaviours, in natural language or as a diagram.
Key relationships
| Idea | What it hides | Java form | Example |
|---|---|---|---|
| Data abstraction | How the data are stored | Attribute (instance or class variable) | private int seatsLeft; |
| Procedural abstraction | How the process works | Method | sellTickets(int count) |
| Instance variable | Nothing: it is per-object state | No static |
each Booth has its own soldHere |
| Class variable | Nothing: it is shared state | static |
one totalSold for all booths |
| Parameter | The specific value used | Variable in the method header | addTax(double amount) |
Assumptions to keep in mind
- A local variable is not an attribute: it lives inside one method or constructor.
- An object’s state is its attributes and their current values.
- A design should list all attributes and behaviours the specification needs, not a sample.
Mistakes to avoid
- Calling a method “abstract” because it has no code. Abstraction is about hiding detail, not missing code.
- Listing only attributes (or only behaviours) in a design.
- Repeating the same block twice instead of writing one method with a parameter.
- Changing a method’s signature while believing it is an “internal” change.
- Treating a class variable as if each object had its own copy.
Mini example to remember
A Screening object has the attributes filmTitle, seatsLeft and price, and the behaviours sellTickets(int), getSeatsLeft() and getPrice(). sellTickets(2) on a screening with 3 seats left returns true and leaves 1; a second sellTickets(2) returns false and leaves 1. The data (attributes) and the process (method) are each given a name, and the callers use them without seeing the insides. In the design diagram, the attributes go in the middle box and the behaviours in the bottom box.
Quick self-check
- A class has
private int lives;and a methodloseLife(). Which is data abstraction and which is procedural abstraction? Answer:livesis data abstraction (an attribute),loseLife()is procedural abstraction (a method). - A method contains the same three lines twice. What should you do and why? Answer: extract them into one method (with parameters if the values differ), to remove duplication and leave one place to fix.
- You speed up a method but keep its header and behaviour. Must callers change? Answer: No.
static int count;appears inPet. If threePetobjects exist, how many copies ofcountexist? Answer: one.