When to use abstract vs concrete in C Sharp (C#) - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how choosing abstract or concrete classes affects the time your program takes to run.
Specifically, how does this choice impact the number of steps your program performs as it grows?
Analyze the time complexity of this example using abstract and concrete classes.
abstract class Animal
{
public abstract void Speak();
}
class Dog : Animal
{
public override void Speak() => Console.WriteLine("Woof");
}
class Program
{
static void MakeAnimalsSpeak(List animals)
{
foreach (var animal in animals)
animal.Speak();
}
}
This code uses an abstract class Animal with a Speak method, and a concrete class Dog that implements it. The program calls Speak on a list of animals.
Look at what repeats when the program runs.
- Primary operation: Calling the Speak method on each animal in the list.
- How many times: Once for each animal in the list (loop runs n times).
As the number of animals grows, the program calls Speak more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to Speak |
| 100 | 100 calls to Speak |
| 1000 | 1000 calls to Speak |
Pattern observation: The number of operations grows directly with the number of animals.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of animals.
[X] Wrong: "Using abstract classes makes the program slower because of extra method calls."
[OK] Correct: The extra method call cost is very small and does not change how the total time grows with input size.
Understanding when to use abstract versus concrete classes shows you can design clear, flexible code without hurting performance as your program grows.
What if we replaced the abstract class with interfaces? How would the time complexity change?
Practice
abstract class in C#?Solution
Step 1: Understand abstract class purpose
An abstract class defines methods or properties that must be implemented by subclasses but does not provide full implementation itself.Step 2: Compare with concrete class
Concrete classes provide full working code and can be instantiated, unlike abstract classes.Final Answer:
When you want to define a common plan without providing full implementation. -> Option AQuick Check:
Abstract class = common plan without full code [OK]
- Confusing abstract with concrete classes
- Thinking abstract classes can be instantiated
- Believing abstract classes provide full method bodies
Solution
Step 1: Recall C# syntax for abstract classes
The correct syntax places the keywordabstractbeforeclassand then the class name.Step 2: Check each option
public abstract class Vehicle { } usespublic abstract class Vehicle { }, which is correct. Other options have incorrect keyword order or missing keywords.Final Answer:
public abstract class Vehicle { } -> Option BQuick Check:
abstract class syntax = 'public abstract class' [OK]
- Placing 'abstract' after 'class'
- Omitting 'class' keyword
- Incorrect keyword order
abstract class Animal {
public abstract void Speak();
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("Woof");
}
}
class Program {
static void Main() {
Animal a = new Dog();
a.Speak();
}
}Solution
Step 1: Understand abstract method implementation
The abstract methodSpeakinAnimalis overridden inDogwith a concrete implementation that prints "Woof".Step 2: Analyze runtime behavior
Creating anAnimalreference to aDogobject and callingSpeak()calls the overridden method, printing "Woof".Final Answer:
Woof -> Option CQuick Check:
Abstract method overridden = prints 'Woof' [OK]
- Thinking abstract class cannot be referenced
- Expecting compile or runtime error
- Assuming abstract methods have bodies
abstract class Shape {
public abstract double Area();
}
class Circle : Shape {
public double Area() {
return 3.14 * 5 * 5;
}
}Solution
Step 1: Check abstract method override rules
When a class inherits an abstract method, it must override it using theoverridekeyword.Step 2: Identify missing override keyword
TheCircleclass definesArea()but missesoverride, causing a compile error.Final Answer:
Circle must declare Area() as override -> Option AQuick Check:
Override keyword required for abstract methods [OK]
- Omitting override keyword
- Thinking abstract methods can be ignored
- Confusing return types
StartEngine(), but the way engines start differs by vehicle type. Which approach is best in C#?Solution
Step 1: Analyze requirement for different implementations
SinceStartEngine()differs by vehicle type, it should be declared abstract to force subclasses to provide their own version.Step 2: Choose correct class design
An abstract classVehiclewith an abstractStartEngine()method fits best, allowing subclasses to implement specific behavior.Final Answer:
Create an abstract class Vehicle with abstract method StartEngine(), then implement it in subclasses. -> Option DQuick Check:
Abstract class for common plan, concrete for specifics [OK]
- Using concrete class with one method for all vehicles
- Trying to put method body in interface (not allowed)
- Using static class which can't be inherited
