Why abstraction is required in Java - Performance Analysis
We want to see how abstraction affects the steps a program takes to run.
How does hiding details change the work done as input grows?
Analyze the time complexity of this simple abstraction example.
public abstract class Vehicle {
abstract void start();
}
public class Car extends Vehicle {
void start() {
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
}
}
This code shows a basic use of abstraction where the Vehicle class hides details of starting.
Look for repeated steps or loops that affect time.
- Primary operation: Calling the start() method on the Vehicle object.
- How many times: Once in this example, but in real programs, it could be many times.
As the number of Vehicle objects grows, each start() call runs once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 start() calls |
| 100 | 100 start() calls |
| 1000 | 1000 start() calls |
Pattern observation: The work grows directly with the number of objects.
Time Complexity: O(n)
This means the time grows linearly with how many times you use the abstraction.
[X] Wrong: "Abstraction makes the program slower because it adds extra steps."
[OK] Correct: Abstraction organizes code but does not add loops or repeated work by itself. The time depends on how many times methods run, not just abstraction.
Understanding how abstraction affects program steps helps you explain design choices clearly and shows you think about efficiency and clarity together.
"What if the start() method called another method inside a loop? How would the time complexity change?"