Abstract methods 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 it takes to run code with abstract methods changes as the program grows.
Specifically, how calling abstract methods affects the number of steps the program takes.
Analyze the time complexity of the following code snippet.
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
This code defines an abstract method and calls it through a subclass instance.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the abstract method
sound()once. - How many times: Exactly one time in this example.
Since the method is called only once, the number of steps stays the same no matter how big the program or input is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The execution steps do not increase with input size here.
Time Complexity: O(1)
This means the time to call an abstract method once stays constant regardless of input size.
[X] Wrong: "Calling an abstract method is slower and grows with input size because it uses dynamic dispatch."
[OK] Correct: Dynamic dispatch adds a tiny fixed cost per call, but it does not grow with input size. The call time stays constant.
Understanding that abstract methods add a fixed call cost helps you explain how object-oriented features affect performance clearly and confidently.
"What if the abstract method is called inside a loop that runs n times? How would the time complexity change?"
Practice
abstract methods in Java is correct?Solution
Step 1: Understand abstract method definition
Abstract methods declare a method signature without a body, forcing subclasses to provide implementation.Step 2: Check each option against the definition
Abstract methods have no body and must be implemented by subclasses. correctly states abstract methods have no body and must be implemented by subclasses. Options A, B, and D are incorrect because abstract methods cannot have bodies, are used in abstract classes (not only interfaces), and cannot be private or final.Final Answer:
Abstract methods have no body and must be implemented by subclasses. -> Option AQuick Check:
Abstract method = no body, must implement [OK]
- Thinking abstract methods can have a body
- Confusing abstract methods with interface methods
- Believing abstract methods can be private or final
Solution
Step 1: Recall abstract method syntax
Abstract methods must be declared with the keywordabstract, have no body, and specify the return type and method name.Step 2: Evaluate each option
public abstract void display(); correctly declares an abstract method with no body. public void abstract display(); incorrectly placesabstractaftervoid. abstract public void display() {} incorrectly provides a method body ({}). void abstract display(); misses the access modifier and placesabstractincorrectly.Final Answer:
public abstract void display(); -> Option DQuick Check:
abstract method = abstract + no body [OK]
- Adding method body {} to abstract methods
- Placing 'abstract' keyword incorrectly
- Omitting access modifiers (though optional)
abstract class Animal {
abstract void sound();
}
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();
}
}Solution
Step 1: Understand class hierarchy and method implementation
ClassAnimalis abstract with an abstract methodsound(). ClassDogextendsAnimaland provides implementation forsound()that prints "Bark".Step 2: Analyze main method execution
Inmain, anAnimalreference points to aDogobject. Callingsound()invokesDog's implementation, printing "Bark".Final Answer:
Bark -> Option BQuick Check:
Abstract method implemented in subclass = prints subclass output [OK]
- Expecting abstract method to run without implementation
- Confusing abstract class instantiation
- Thinking abstract method prints default text
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
class Square extends Shape {
}
public class Test {
public static void main(String[] args) {
Shape s = new Square();
s.draw();
}
}Solution
Step 1: Check abstract method implementation in subclasses
ClassShapehas abstract methoddraw().Circlecorrectly implements it.Squaredoes not implementdraw().Step 2: Understand consequences of missing implementation
SinceSquaredoes not implement the abstract method, it must be declared abstract or implement the method. Otherwise, compilation error occurs.Final Answer:
Class Square must implement the abstract method draw() -> Option CQuick Check:
Subclass must implement all abstract methods [OK]
- Forgetting to implement abstract methods in subclasses
- Trying to instantiate abstract classes directly
- Misunderstanding abstract method requirements
startEngine() behavior. Which approach using abstract methods is best to ensure this?Solution
Step 1: Understand requirement for mandatory implementation
The program requires each vehicle type to provide its ownstartEngine()behavior, so subclasses must be forced to implement this method.Step 2: Evaluate options for enforcing implementation
Create an abstract class Vehicle with an abstract method startEngine(), then subclass it for each vehicle type implementing startEngine(). uses an abstract class with an abstract method, forcing subclasses to implementstartEngine(). Create a concrete class Vehicle with a startEngine() method that prints a generic message, and override it in subclasses. provides a generic method that can be overridden but does not force implementation. Create an interface Vehicle with a default startEngine() method, and override it in subclasses if needed. uses an interface with a default method, which subclasses may skip overriding. Create a class Vehicle with a private startEngine() method and call it from subclasses. uses a private method, which is not accessible to subclasses.Final Answer:
Create an abstract class Vehicle with an abstract method startEngine(), then subclass it for each vehicle type implementing startEngine(). -> Option AQuick Check:
Abstract method forces subclass implementation [OK]
- Using concrete methods without forcing override
- Using private methods inaccessible to subclasses
- Relying on default interface methods without enforcement
