#OOP #abstraction [[Outcome 1 - Programming#7. principles of OOP, including| U3 - AOS1 - KK7]] ## What is Abstraction? To understand abstraction, we first need to look away from the computer and look at a car. Imagine you are driving. To make the car move, you press the accelerator. To stop, you press the brake. To turn, you use the steering wheel. **What you DON'T do:** - You don't manually inject fuel into the cylinders. - You don't personally calculate the air-to-fuel ratio. - You don't squeeze the brake pads against the rotors with your bare hands. **The Lesson:** As a driver, you only interact with the **interface** (pedals, wheel). The complex mechanics (the **implementation**) are hidden away under the hood. **This is Abstraction.** It is the process of hiding complex implementation details and showing only the necessary features of an object. ![[image 1.png]] ### You have already used abstraction In fact you have already used abstraction in your code. See this python code below ```python data = [1,2,3] data.append[4] ``` In this code, we are appending a new number to the list. You simply type `.append(4)`, and it works. This is abstraction in action. #### **What you see (The Interface):** You provide the data (`4`), and the list accepts it. It is a single, simple line of code. #### **What is hidden (The Implementation):** Under the hood, Python is doing a massive amount of complex work that you don't see: 1. It checks if there is enough reserved memory space next to the current list to fit the new number. 2. If there isn't enough space, Python has to find a new, larger chunk of memory RAM. 3. It then copies all the old numbers (`1, 2, 3`) to this new location. 4. Finally, it adds the `4` to the end and deletes the old list. Because of **abstraction**, you don't have to write the code to find RAM addresses or copy data bytes. You just trust the `.append()` method to handle the "how" while you focus on the "what." ## The Definition In Computer Science terms: > **Abstraction** is the principle of focusing on **what** an object does, rather than **how** it does it. We treat complex code as a "Black Box." We know what goes in (arguments) and what comes out (return values), but we don't need to stress about the code inside the box every time we use it. ## Abstraction in OOP (The Technical Layer) In Software Development, we use **Classes** to create abstraction. When you create a Class, you are deciding what to expose to the public (other programmers or parts of your program) and what to keep private. - **Public Interface:** The methods users call (e.g., `car.drive()`). - **Private Implementation:** The logic and variables inside the method that make it work (e.g., specific math, database connections, hardware signals). ### Why do we care? For the VCE Software Development SAT (School Assessed Task), abstraction is crucial because: 1. **Simplicity:** It reduces cognitive load. You can build a complex app without thinking about every single line of code at once. 2. **Maintainability:** If you change _how_ the engine works (implementation), the driver doesn't need to relearn how to drive (interface). You can fix bugs inside a method without breaking the rest of the program. ## Let's look at a "Coffee Machine" object. **Without Abstraction (Spaghetti Code):** Every time you want coffee, you have to write code to heat the water, grind beans, press the water, and pour it. With Abstraction (OOP): We wrap that complexity inside a method. ```Python class CoffeeMachine: def __init__(self): self.water_level = 100 self.beans = 50 # INTERNAL METHOD (The complex part we want to hide) # In Python, we often use an underscore '_' to suggest it's internal/private def _boil_water(self): print("Heating element activated...") print("Water reached 96 degrees Celsius.") def _grind_beans(self): print("Grinding beans to medium-fine...") # PUBLIC METHOD (The Interface) # This is the only thing the user needs to know about! def make_coffee(self): if self.water_level > 0: print("Starting brew process...") self._boil_water() # Hiding the complex boiling logic self._grind_beans() # Hiding the complex grinding logic print("Coffee is ready! ☕") self.water_level -= 20 else: print("Refill water!") # --- The Main Program --- my_machine = CoffeeMachine() # The user only calls the simple abstraction: my_machine.make_coffee() ``` **Analysis**: The user simply calls my_machine.make_coffee(). They don't need to know that _boil_water() or _grind_beans() even exist. We have abstracted the complexity away.