0
0
C Sharp (C#)programming~10 mins

Multiple interface implementation in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple interface implementation
Define Interface A
Define Interface B
Create Class implementing A and B
Implement methods from A and B
Create object of Class
Call methods from both interfaces
Use combined behavior
This flow shows how a class can implement multiple interfaces by defining all required methods, then using an object to access all interface behaviors.
Execution Sample
C Sharp (C#)
using System;
interface IA { void MethodA(); }
interface IB { void MethodB(); }
class MyClass : IA, IB {
  public void MethodA() { Console.WriteLine("A"); }
  public void MethodB() { Console.WriteLine("B"); }
}
class Program {
  static void Main() {
    var obj = new MyClass();
    obj.MethodA(); obj.MethodB();
  }
}
This code defines two interfaces and a class that implements both, then calls their methods.
Execution Table
StepActionEvaluationResult
1Define interface IAInterface IA with MethodA declaredIA ready
2Define interface IBInterface IB with MethodB declaredIB ready
3Define class MyClass implementing IA and IBClass must implement MethodA and MethodBMyClass ready
4Implement MethodA in MyClassMethodA prints 'A'MethodA implemented
5Implement MethodB in MyClassMethodB prints 'B'MethodB implemented
6Create object obj of MyClassobj is instance of MyClassobj created
7Call obj.MethodA()Calls MethodAOutput: A
8Call obj.MethodB()Calls MethodBOutput: B
9End of executionAll interface methods calledProgram ends
💡 All interface methods implemented and called, program ends normally
Variable Tracker
VariableStartAfter 6After 7After 8Final
objnullMyClass instanceMyClass instanceMyClass instanceMyClass instance
Key Moments - 2 Insights
Why must MyClass implement all methods from both interfaces?
Because in row 3 of execution_table, the class declares it implements IA and IB, so it must provide all methods declared in both interfaces to compile.
Can we call MethodA and MethodB on obj directly?
Yes, as shown in rows 7 and 8, obj is a MyClass instance that implements both interfaces, so both methods are accessible.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does MethodA do?
APrints 'A' to the console
BPrints 'B' to the console
CThrows an error
DDoes nothing
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 4 in execution_table
At which step is the object 'obj' created?
AStep 3
BStep 7
CStep 6
DStep 1
💡 Hint
Look for the action mentioning 'Create object obj' in execution_table
If MyClass did not implement MethodB, what would happen?
AProgram runs normally
BCompile-time error occurs
CMethodB calls print 'A'
DRuntime error occurs
💡 Hint
Refer to key_moments about implementing all interface methods
Concept Snapshot
Multiple interface implementation in C#:
- Use class : Interface1, Interface2
- Implement all interface methods
- Create object of class
- Call methods from all interfaces
- Enables combining behaviors from multiple sources
Full Transcript
This example shows how a class in C# can implement multiple interfaces by listing them separated by commas after the colon. The class must provide all methods declared in each interface. We define two interfaces IA and IB, each with one method. Then we create MyClass implementing both. It provides MethodA and MethodB. We create an object of MyClass and call both methods, printing 'A' and 'B'. This demonstrates how multiple interface implementation allows a class to combine behaviors from different contracts.