Multiple interface implementation in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a class implements multiple interfaces, it must provide code for each interface's methods. We want to understand how this affects the time it takes to run the program.
How does adding more interfaces change the work the program does?
Analyze the time complexity of the following code snippet.
interface IA { void MethodA(); }
interface IB { void MethodB(); }
class MyClass : IA, IB {
private int n;
public MyClass(int n) { this.n = n; }
public void MethodA() {
for (int i = 0; i < n; i++) {
// some operation
}
}
public void MethodB() {
for (int j = 0; j < n; j++) {
// some operation
}
}
}
This class implements two interfaces, each with a method that loops n times doing some work.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops, each running n times.
- How many times: Each loop runs independently n times when its method is called.
Each method runs a loop that grows with n. If both methods are called once, total work is about 2 times n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 + 10) |
| 100 | About 200 operations (100 + 100) |
| 1000 | About 2000 operations (1000 + 1000) |
Pattern observation: The total work grows linearly with n, doubling because of two methods.
Time Complexity: O(n)
This means the total work grows in a straight line as n grows, even with multiple interfaces.
[X] Wrong: "Implementing more interfaces always makes the program slower by a lot."
[OK] Correct: Each interface method runs separately, so total work adds up linearly, not exponentially or worse.
Understanding how multiple interfaces affect time helps you explain how your code scales. It shows you can think about how adding features changes performance.
"What if each interface method called the other method inside its loop? How would the time complexity change?"
Practice
What does it mean when a C# class implements multiple interfaces?
Solution
Step 1: Understand interface implementation
Interfaces define method signatures but no code. A class implementing them must provide the code.Step 2: Multiple interfaces require all methods
When a class implements several interfaces, it must write code for every method in all interfaces.Final Answer:
The class agrees to provide code for all methods defined in those interfaces. -> Option BQuick Check:
Multiple interface implementation = implement all methods [OK]
- Thinking interfaces provide code to inherit
- Believing class can skip some interface methods
- Confusing interfaces with classes
Which of the following is the correct syntax to declare a class Car implementing interfaces IMovable and IEngine?
?
Solution
Step 1: Recall C# interface syntax
In C#, a class uses a colon ':' followed by interface names separated by commas.Step 2: Check each option
public class Car : IMovable, IEngine { } uses ':' and commas correctly. Options B and C use wrong keywords. public class Car : IMovable & IEngine { } uses '&' which is invalid.Final Answer:
public class Car : IMovable, IEngine { } -> Option AQuick Check:
Interfaces listed after ':' separated by commas [OK]
- Using 'implements' keyword (Java style)
- Using '&' instead of commas
- Using 'inherits' keyword incorrectly
What will be the output of the following C# code?
interface IA { void Show(); }
interface IB { void Show(); }
class Demo : IA, IB {
public void Show() { Console.WriteLine("Hello"); }
}
class Program {
static void Main() {
Demo d = new Demo();
d.Show();
}
}Solution
Step 1: Understand method implementation for multiple interfaces
Both interfaces have Show method. The class Demo implements one Show method that satisfies both.Step 2: Check program output
Main creates Demo and calls Show, which prints "Hello".Final Answer:
Hello -> Option AQuick Check:
Single method implements both interfaces' Show [OK]
- Expecting compile error for same method name
- Thinking separate methods needed for each interface
- Confusing interface method calls
Identify the error in this code snippet:
interface IA { void Run(); }
interface IB { void Jump(); }
class Player : IA, IB {
public void Run() { Console.WriteLine("Running"); }
}Solution
Step 1: Check interface methods
IA requires Run(), IB requires Jump().Step 2: Verify class implementation
Player implements Run() but misses Jump(), so it is incomplete.Final Answer:
Class Player must implement Jump method from IB interface. -> Option DQuick Check:
All interface methods must be implemented [OK]
- Forgetting to implement all interface methods
- Thinking interfaces can't have methods
- Assuming methods can be private
Given these interfaces and class:
interface IAlpha { void Action(); }
interface IBeta { void Action(); }
class Combined : IAlpha, IBeta {
void IAlpha.Action() { Console.WriteLine("Alpha Action"); }
void IBeta.Action() { Console.WriteLine("Beta Action"); }
}
class Program {
static void Main() {
Combined c = new Combined();
// Which calls are valid?
}
}Which of the following calls will compile and print output?
Solution
Step 1: Understand explicit interface implementation
Combined class implements IAlpha.Action and IBeta.Action explicitly, so these methods are not accessible via class instance directly.Step 2: Check method calls
Only calls through interface references like (IAlpha)c or (IBeta)c are valid. c.Action() is invalid and causes compile error.Final Answer:
((IAlpha)c).Action(); // prints 'Alpha Action' -> Option CQuick Check:
Explicit interface methods need interface cast to call [OK]
- Calling explicit interface methods directly on class instance
- Confusing explicit and implicit implementation
- Assuming c.Action() works without cast
