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.
Partial abstraction in 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.
abstract class Animal { abstract void makeSound(); } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } }
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."); } }
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; } }
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.
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(); } }
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.
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.