Β 

🎭 Abstraction in Java – Hiding Details, Showing Essentials

When you drive a car πŸš—, do you worry about how the engine works internally?
No! You only care about the steering wheel, brakes, accelerator, and gears.
πŸ‘‰ That’s exactly what Abstraction does in Java: it hides implementation details and shows only necessary functionality.


🌟 What is Abstraction?

  • Definition: Abstraction is the process of hiding the internal details of an object and exposing only the essential features.

  • In Java, abstraction is achieved using:
    βœ… Abstract Classes (0–100% abstraction)
    βœ… Interfaces (100% abstraction, before Java 8; now supports default & static methods too)

Β 

πŸ“˜ What is an Abstract Class in Java?

An abstract class in Java is a special type of class that is incomplete and cannot be instantiated directly.
πŸ‘‰ Think of it as a blueprint for other classes.

It’s used when you want to:

  • Define common properties and behaviors for all subclasses.

  • Leave some methods unimplemented (to be completed by subclasses).

✨ Key Features of Abstract Class

  • Declared using the abstract keyword.

  • Can have:
    βœ… Abstract methods β†’ methods without a body (only declaration).
    βœ… Concrete methods β†’ methods with a body (implementation).

  • Cannot be used to create objects directly.

  • Must be extended by another class that provides implementation.


πŸ› οΈ Example: Abstract Class

// Abstract Class
abstract class Animal {
    // Abstract method (no body)
    abstract void sound();

    // Concrete method
    void sleep() {
        System.out.println("Animal is sleeping");
    }
}

// Subclass
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        // Animal a = new Animal(); ❌ Not allowed (abstract class)
        Animal a = new Dog();  // βœ… Polymorphism
        a.sound();  // Dog barks
        a.sleep();  // Animal is sleeping
    }
}

Output:

Dog barks
Animal is sleeping

🎯 When to Use Abstract Class?

  • When you want to share common code among multiple subclasses.

  • When you want to force subclasses to implement certain methods.

  • When you need partial abstraction (some methods defined, some abstract).

🌍 Real-World Example

  • Shape Class 🟦:

    • Abstract Class: Shape (common property: area, perimeter).

    • Subclasses: Circle, Rectangle, Triangle β†’ each implements its own way to calculate area.

πŸ‘‰ In short:
Abstract class = β€œTemplate + Common Code”
It allows flexibility while ensuring subclasses follow a certain structure.

Another example on abstract class:

abstract class Vehicle {
    abstract void start();  // Abstract method (no body)
    
    void fuel() {           // Concrete method
        System.out.println("Vehicle needs fuel");
    }
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car starts with a key");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();  // Calls Car's implementation
        v.fuel();
    }
}

Output:

Car starts with a key
Vehicle needs fuel

πŸ”‘ What is an Interface in Java?

An interface in Java is like a contract or blueprint that defines a set of methods (without implementation) that a class must follow.

πŸ‘‰ Think of it as a 100% abstraction tool (before Java 8) β€” it tells what a class should do, not how it should do it.


✨ Key Features of Interfaces

  • Declared using the interface keyword.

  • All methods are public and abstract by default (till Java 7).

  • From Java 8 onwards:
    βœ… Can have default methods (with body).
    βœ… Can have static methods.

  • Supports multiple inheritance (a class can implement multiple interfaces).

  • Cannot have constructors (cannot be instantiated).


πŸ› οΈ Example: Interface in Action

// Interface
interface Animal {
    void sound();   // abstract method
    void sleep();   // abstract method
}

// Class implementing interface
class Dog implements Animal {
    public void sound() {
        System.out.println("Dog barks");
    }
    public void sleep() {
        System.out.println("Dog sleeps");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();  // βœ… Polymorphism
        a.sound();  // Dog barks
        a.sleep();  // Dog sleeps
    }
}

Output:

Dog barks
Dog sleeps

🎯 When to Use Interface?

  • When you want to achieve abstraction but don’t need shared implementation.

  • When you want to implement multiple inheritance (since Java doesn’t support multiple class inheritance).

  • When different classes must follow the same contract, but may implement it differently.

🌍 Real-World Example

  • Payment System πŸ’³:

    • Interface: Payment (methods: pay(), refund()).

    • Implementations: CreditCard, PayPal, UPI.

    • Each class defines how payment is processed, but all must follow the same contract.

πŸ‘‰ In short:
Interface = 100% abstraction + multiple inheritance support.
It defines a “what-to-do” list that every implementing class must complete.

Β 

🎯 Key Points to Remember

  • Abstract class can have both abstract methods (without body) and concrete methods (with body).

  • Interface provides pure abstraction (all methods abstract by default, unless default/static).

  • Abstraction helps in security (hiding implementation), flexibility, and code reusability.

🌍 Real-World Analogy

  • ATM Machine πŸ’³: You only see options like “Withdraw”, “Deposit”, “Check Balance” β†’ You don’t see how it internally interacts with the bank server.

  • Mobile Phone πŸ“±: You just click a button to take a picture; the complex camera algorithms are abstracted away.

✨ Bottom line: Abstraction = β€œDon’t tell me how, just tell me what.”
It makes code cleaner, modular, and user-friendly.

Β