Java Abstraction

Hiding implementation details while showing essential features

🎭 What is Abstraction?

Abstraction hides complex implementation details and shows only essential features to users. It focuses on what an object does rather than how it does it, making code simpler and more manageable.


// Abstract class example
abstract class Animal {
    abstract void makeSound(); // Abstract method
    void sleep() { System.out.println("Sleeping..."); } // Concrete method
}
                                    

Types of Abstraction

🏛️

Abstract Classes

Classes that cannot be instantiated

abstract class Shape {
    abstract double area();
}
📋

Abstract Methods

Methods without implementation

abstract void draw();
abstract int calculate();
🔧

Concrete Methods

Methods with implementation

void display() {
    System.out.println("Displaying...");
}
👥

Inheritance

Child classes implement abstract methods

class Circle extends Shape {
    double area() { return 3.14 * r * r; }
}

🔹 Abstract Class Example

Here's a complete example of abstraction using abstract classes:

// Abstract class
abstract class Vehicle {
    String brand;
    
    // Constructor
    Vehicle(String brand) {
        this.brand = brand;
    }
    
    // Abstract method - must be implemented by subclasses
    abstract void start();
    
    // Concrete method - can be used by all subclasses
    void stop() {
        System.out.println(brand + " vehicle stopped");
    }
}

// Concrete class extending abstract class
class Car extends Vehicle {
    Car(String brand) {
        super(brand);
    }
    
    // Implementing abstract method
    void start() {
        System.out.println(brand + " car started with key");
    }
}

class Bike extends Vehicle {
    Bike(String brand) {
        super(brand);
    }
    
    void start() {
        System.out.println(brand + " bike started with kick");
    }
}

Usage:

Car car = new Car("Toyota");
car.start(); // Output: Toyota car started with key
car.stop();  // Output: Toyota vehicle stopped

Bike bike = new Bike("Honda");
bike.start(); // Output: Honda bike started with kick

🔹 Key Rules of Abstraction

Important rules to remember when working with abstract classes:

Abstract Class Rules:

  • Cannot be instantiated: You cannot create objects of abstract classes
  • Can have constructors: Used by subclasses via super()
  • Can have concrete methods: Methods with implementation
  • Must be extended: Subclasses must implement abstract methods
  • Can have instance variables: Both static and non-static
// This will cause compilation error
// Vehicle v = new Vehicle("Generic"); // Cannot instantiate

// This is correct
Car myCar = new Car("BMW");
Vehicle myVehicle = new Car("Audi"); // Polymorphism

🔹 Real-World Example

A practical example using abstract classes for a drawing application:

abstract class Shape {
    protected String color;
    
    Shape(String color) {
        this.color = color;
    }
    
    // Abstract methods
    abstract double calculateArea();
    abstract void draw();
    
    // Concrete method
    void setColor(String color) {
        this.color = color;
        System.out.println("Color changed to " + color);
    }
}

class Rectangle extends Shape {
    private double length, width;
    
    Rectangle(String color, double length, double width) {
        super(color);
        this.length = length;
        this.width = width;
    }
    
    double calculateArea() {
        return length * width;
    }
    
    void draw() {
        System.out.println("Drawing " + color + " rectangle");
    }
}

class Circle extends Shape {
    private double radius;
    
    Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }
    
    double calculateArea() {
        return 3.14159 * radius * radius;
    }
    
    void draw() {
        System.out.println("Drawing " + color + " circle");
    }
}

Output:

Rectangle rect = new Rectangle("Red", 5, 3);
rect.draw(); // Drawing Red rectangle
System.out.println("Area: " + rect.calculateArea()); // Area: 15.0

Circle circle = new Circle("Blue", 4);
circle.draw(); // Drawing Blue circle
System.out.println("Area: " + circle.calculateArea()); // Area: 50.26536

🧠 Test Your Knowledge

Can you create an object of an abstract class?