Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does multiple interface implementation mean in C#?
It means a class can use more than one interface at the same time, allowing it to promise to do multiple sets of actions.
Click to reveal answer
beginner
How do you declare a class that implements two interfaces named <code>IWalk</code> and <code>IRun</code>?
Use a comma to separate interfaces like this:<br><pre>class Animal : IWalk, IRun { }</pre>
Click to reveal answer
intermediate
Can a class implement two interfaces that have methods with the same name?
Yes, but you may need to use explicit interface implementation to avoid confusion and specify which method belongs to which interface.
Click to reveal answer
intermediate
What is explicit interface implementation in multiple interface implementation?
It is a way to write methods so they only work when accessed through the interface, helping to handle method name conflicts.
Click to reveal answer
beginner
Why is multiple interface implementation useful?
It helps a class to follow many different contracts or roles, making code flexible and easier to organize.
Click to reveal answer
How do you declare a class that implements two interfaces IRead and IWrite?
Aclass File implements IRead, IWrite { }
Bclass File : IRead & IWrite { }
Cclass File : IRead, IWrite { }
Dclass File : IRead.IWrite { }
✗ Incorrect
In C#, use a colon and separate interfaces with commas: class File : IRead, IWrite { }.
If two interfaces have a method with the same name, how can a class implement both?
AUse explicit interface implementation
BRename one method in the class
CYou cannot implement both
DUse inheritance instead
✗ Incorrect
Explicit interface implementation lets you specify which method belongs to which interface.
Which keyword is used to implement interfaces in a class?
A:
Bimplements
Cextends
Dinherits
✗ Incorrect
C# uses a colon (:) to implement interfaces in a class.
Can a class implement zero or more interfaces?
ANo, at least one
BYes, zero or more
CNo, only one
DOnly zero
✗ Incorrect
A class can implement no interfaces or many interfaces.
What happens if a class implements an interface but does not provide all methods?
AInterface methods are ignored
BIt works fine
CRuntime error
DCompilation error
✗ Incorrect
C# requires all interface methods to be implemented, or the code will not compile.
Explain how multiple interface implementation works in C# and why it is useful.
Think about how a class can promise to do many things by following many contracts.
You got /4 concepts.
Describe explicit interface implementation and when you would use it.
Consider when two interfaces have the same method name.
You got /4 concepts.
Practice
(1/5)
1.
What does it mean when a C# class implements multiple interfaces?
easy
A. The class inherits code from multiple classes.
B. The class agrees to provide code for all methods defined in those interfaces.
C. The class can only use one interface at a time.
D. The class automatically gets all properties from the interfaces without coding.
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 B
Quick Check:
Multiple interface implementation = implement all methods [OK]
Hint: Interfaces are contracts; class must fulfill all method contracts [OK]
Common Mistakes:
Thinking interfaces provide code to inherit
Believing class can skip some interface methods
Confusing interfaces with classes
2.
Which of the following is the correct syntax to declare a class Car implementing interfaces IMovable and IEngine?
?
easy
A. public class Car : IMovable, IEngine { }
B. public class Car implements IMovable, IEngine { }
C. public class Car inherits IMovable, IEngine { }
D. public class Car : IMovable & 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 A
Quick Check:
Interfaces listed after ':' separated by commas [OK]
Hint: Use ':' and commas to list interfaces after class name [OK]
Common Mistakes:
Using 'implements' keyword (Java style)
Using '&' instead of commas
Using 'inherits' keyword incorrectly
3.
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();
}
}
medium
A. Hello
B. Compilation error due to ambiguous Show method
C. Runtime error
D. No output
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 A
Quick Check:
Single method implements both interfaces' Show [OK]
Hint: One method can implement same method from multiple interfaces [OK]
Common Mistakes:
Expecting compile error for same method name
Thinking separate methods needed for each interface
Confusing interface method calls
4.
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"); }
}
medium
A. Run method should be private.
B. Class Player cannot implement two interfaces.
C. Interfaces cannot have methods.
D. Class Player must implement Jump method from IB interface.
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 D
Quick Check:
All interface methods must be implemented [OK]
Hint: Implement all interface methods to avoid errors [OK]
Common Mistakes:
Forgetting to implement all interface methods
Thinking interfaces can't have methods
Assuming methods can be private
5.
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?