0
0
Javaprogramming~7 mins

Partial abstraction in Java

Choose your learning style9 modes available
Introduction

Partial abstraction lets you hide some details of a class but still keep some parts to be defined later. It helps organize code and share common behavior while forcing subclasses to fill in the missing pieces.

When you want to create a base class that cannot be used directly but provides common code for subclasses.
When some methods should have a default implementation but others must be implemented by subclasses.
When you want to design a framework or library where users extend your classes and provide specific behavior.
When you want to enforce a contract for subclasses while sharing code in the parent class.
Syntax
Java
abstract class Vehicle {
    // Abstract method (no body)
    abstract void startEngine();

    // Concrete method (has body)
    void stopEngine() {
        System.out.println("Engine stopped.");
    }
}

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

You cannot create objects of an abstract class directly.

Examples
Abstract class Animal has an abstract method makeSound(). Dog must implement makeSound() or it will be abstract too.
Java
abstract class Animal {
    abstract void makeSound();
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Bark");
    }
}
Appliance has an abstract method turnOn() and a concrete method plugIn(). Fan implements turnOn() and inherits plugIn().
Java
abstract class Appliance {
    abstract void turnOn();
    void plugIn() {
        System.out.println("Plugged in.");
    }
}

class Fan extends Appliance {
    void turnOn() {
        System.out.println("Fan is running.");
    }
}
Shape defines an abstract method area() and a concrete method display(). Circle implements area() and inherits display().
Java
abstract class Shape {
    abstract double area();
    void display() {
        System.out.println("Displaying shape");
    }
}

class Circle extends Shape {
    double radius;
    Circle(double radius) {
        this.radius = radius;
    }
    double area() {
        return Math.PI * radius * radius;
    }
}
Sample Program

This program shows partial abstraction. Vehicle is abstract with one abstract method and one concrete method. Car implements the abstract method. We cannot create Vehicle objects but can create Car objects.

Java
abstract class Vehicle {
    abstract void startEngine();

    void stopEngine() {
        System.out.println("Engine stopped.");
    }
}

class Car extends Vehicle {
    @Override
    void startEngine() {
        System.out.println("Car engine started.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Vehicle v = new Vehicle(); // Not allowed, Vehicle is abstract
        Car myCar = new Car();
        myCar.startEngine();
        myCar.stopEngine();
    }
}
OutputSuccess
Important Notes

Time complexity: Partial abstraction itself does not affect time complexity; it is about design.

Space complexity: No extra space cost compared to normal classes.

Common mistake: Trying to create an object of an abstract class causes a compile error.

Use partial abstraction when you want to share code but force subclasses to implement specific methods.

Summary

Partial abstraction means an abstract class can have both abstract and concrete methods.

Abstract classes cannot be instantiated directly.

Subclasses must implement all abstract methods or be abstract themselves.