← KyΕ«kei Master Hub
SCS2108 | Phase Study Card
πŸ‘οΈ -- opens | 0%

Phase 10: Deep Dive: How Abstract Classes & Interfaces Implement Abstraction

The Lightbulb Moment: Abstraction is the concept/goal. Abstract Classes and Interfaces are the exact tools Java gives you to build Abstraction!

1. Connecting the Dots: What is Abstraction Really?

πŸ’‘ The Goal vs The Tool

Think of Abstraction as a spectrum of hiding internal mechanics while exposing a clean public contract:

  • 0% Abstraction (Concrete Class): All methods have complete code bodies (`class Car`). Nothing is abstracted.
  • Partial Abstraction (Abstract Class): Some methods have code, but others are left abstract (`abstract class GraphicObject`). You get shared features + forced customization!
  • 100% Pure Abstraction (Interface): ZERO internal code. Pure contract (`interface Printable`). It specifies WHAT must be done, leaving HOW 100% to implementing classes.

2. Abstract Classes = Partial Abstraction (Shared Base + Forced Rules)

Use an **Abstract Class** when classes share common code/attributes, but specific key behaviors MUST be customized by subclasses.

// Abstract Class (Partial Abstraction) public abstract class GraphicObject { protected int x, y; // 1. Shared state field public void moveTo(int newX, int newY) { // 2. Concrete method (0% abstraction here) this.x = newX; this.y = newY; } public abstract void draw(); // 3. Abstract method (100% abstraction: MUST be overridden!) } // Subclass fulfills the abstraction contract public class Circle extends GraphicObject { @Override public void draw() { System.out.println("Drawing a circle at (" + x + "," + y + ")"); } }

3. Interfaces = 100% Pure Abstraction (Universal Socket)

πŸ”Œ The Universal Wall Socket Analogy

A wall socket is an interface Socket. It declares a contract: "Anything with a 2-pin plug can draw 220V electricity."
A `Television`, a `Microwave`, and a `LaptopCharger` are completely unrelated classes, but they all implements Socket. You (the user) just plug them inβ€”that is Pure Abstraction!

// Pure Interface (100% Contract) public interface Flyable { void fly(); // Abstract method declaration } public class Airplane implements Flyable { public void fly() { System.out.println("Flying using jet engines!"); } } public class Bird implements Flyable { public void fly() { System.out.println("Flying by flapping wings!"); } }

4. Abstraction Spectrum Summary Table

Mechanism Level of Abstraction Primary Architectural Purpose
Concrete Class 0% Abstraction Instantiation of real objects with full code logic.
Abstract Class Partial Abstraction (0% - 100%) Shared code reusability & state among closely related IS-A subclasses.
Interface 100% Pure Abstraction Enforcing a common capability CAN-DO contract across unrelated classes.
πŸ’‘ Examiner Distinction Tip
When examiners ask: "How does Java achieve Abstraction?"
Your answer MUST mention: 1. Abstract Classes (partial abstraction via abstract methods) and 2. Interfaces (100% contract abstraction)!