Partial abstraction in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to run code with partial abstraction changes as input grows.
How does the use of abstract methods affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
double radius;
Circle(double r) { radius = r; }
double area() { return Math.PI * radius * radius; }
}
// Using the area method on a list of shapes
for (Shape s : shapes) {
double a = s.area();
}
This code calls an abstract method area() on each shape in a list, using partial abstraction.
Look for loops or repeated calls that affect time.
- Primary operation: Calling
area()on each shape object. - How many times: Once per shape in the list, so as many times as the list size.
Each shape in the list requires one call to area(). As the list grows, calls grow the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to area() |
| 100 | 100 calls to area() |
| 1000 | 1000 calls to area() |
Pattern observation: The number of operations grows directly with the number of shapes.
Time Complexity: O(n)
This means the time grows linearly with the number of shapes processed.
[X] Wrong: "Using abstract methods makes the code slower by a lot because of extra overhead."
[OK] Correct: The overhead of calling an abstract method is very small and does not change the overall linear growth with input size.
Understanding how abstraction affects time helps you explain design choices clearly and shows you know both code structure and performance.
What if the area() method itself contained a loop over a large data structure? How would the time complexity change?
Practice
What does partial abstraction mean in Java?
Solution
Step 1: Understand abstract class capabilities
Partial abstraction means an abstract class can have some methods with implementation (concrete) and some without (abstract).Step 2: Compare options with definition
Only An abstract class can have both abstract and concrete methods. correctly states this; others are incorrect or false statements.Final Answer:
An abstract class can have both abstract and concrete methods. -> Option AQuick Check:
Partial abstraction = abstract + concrete methods [OK]
- Thinking abstract classes must have only abstract methods
- Believing abstract classes can be instantiated
- Confusing static methods with abstract methods
Which of the following is the correct way to declare an abstract method inside an abstract class?
public abstract class Shape {
?
}Solution
Step 1: Recall abstract method syntax
Abstract methods must be declared with the 'abstract' keyword and no body, and usually have a visibility modifier.Step 2: Check each option
public abstract void draw(); correctly declares 'public abstract void draw();'. public void draw(); misses 'abstract', C misses visibility, D has a method body which is invalid for abstract methods.Final Answer:
public abstract void draw(); -> Option DQuick Check:
Abstract method = 'public abstract' + no body [OK]
- Omitting the 'abstract' keyword
- Providing a method body for abstract methods
- Missing visibility modifier
What will be the output of the following code?
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Sleeping");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
a.sleep();
}
}Solution
Step 1: Understand method calls on abstract class reference
Variable 'a' is of type Animal but refers to Dog instance. Calling 'sound()' invokes Dog's override, printing 'Bark'. Calling 'sleep()' uses Animal's concrete method, printing 'Sleeping'.Step 2: Check output order
First 'a.sound()' prints 'Bark', then 'a.sleep()' prints 'Sleeping'.Final Answer:
Bark Sleeping -> Option CQuick Check:
Abstract ref calls subclass method then superclass concrete method [OK]
- Expecting abstract class to instantiate directly
- Confusing method call order
- Thinking abstract methods have implementation
Identify the error in the following code snippet:
abstract class Vehicle {
abstract void move();
}
class Car extends Vehicle {
void start() {
System.out.println("Car started");
}
}Solution
Step 1: Check abstract method implementation
Vehicle declares abstract method move(). Any concrete subclass must implement it or be declared abstract.Step 2: Analyze Car class
Car does not implement move() and is not abstract, so this causes a compilation error.Final Answer:
Car class must implement the move() method or be abstract. -> Option AQuick Check:
Concrete subclass must implement all abstract methods [OK]
- Forgetting to implement abstract methods in subclass
- Thinking abstract class can't have abstract methods
- Believing subclass can skip abstract methods without being abstract
Given the abstract class below, which subclass implementation correctly uses partial abstraction?
abstract class Appliance {
abstract void turnOn();
void plugIn() {
System.out.println("Plugged in");
}
}Choose the correct subclass:
Solution
Step 1: Check abstract method implementation
Subclass must implement abstract method turnOn() to be concrete.Step 2: Analyze each subclass option
class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } } implements turnOn() only, using inherited plugIn() as is, which is valid partial abstraction. class Fan extends Appliance { void plugIn() { System.out.println("Fan plugged in"); } } misses turnOn(), causing error. class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } void plugIn() { System.out.println("Fan plugged in"); } } overrides plugIn() unnecessarily but is valid. class Fan extends Appliance {} misses turnOn(), causing error.Final Answer:
class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } } -> Option BQuick Check:
Implement abstract methods, inherit concrete ones [OK]
- Not implementing abstract methods in subclass
- Overriding concrete methods unnecessarily
- Leaving subclass empty without abstract method implementation
