Bird
Raised Fist0
C Sharp (C#)programming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

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
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.

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

  1. Step 1: Understand interface implementation

    Interfaces define method signatures but no code. A class implementing them must provide the code.
  2. Step 2: Multiple interfaces require all methods

    When a class implements several interfaces, it must write code for every method in all interfaces.
  3. Final Answer:

    The class agrees to provide code for all methods defined in those interfaces. -> Option B
  4. 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

  1. Step 1: Recall C# interface syntax

    In C#, a class uses a colon ':' followed by interface names separated by commas.
  2. 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.
  3. Final Answer:

    public class Car : IMovable, IEngine { } -> Option A
  4. 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

  1. Step 1: Understand method implementation for multiple interfaces

    Both interfaces have Show method. The class Demo implements one Show method that satisfies both.
  2. Step 2: Check program output

    Main creates Demo and calls Show, which prints "Hello".
  3. Final Answer:

    Hello -> Option A
  4. 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

  1. Step 1: Check interface methods

    IA requires Run(), IB requires Jump().
  2. Step 2: Verify class implementation

    Player implements Run() but misses Jump(), so it is incomplete.
  3. Final Answer:

    Class Player must implement Jump method from IB interface. -> Option D
  4. 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?

hard
A. ((IBeta)c).Action(); // prints 'Beta Action'
B. c.Action(); // prints 'Alpha Action'
C. ((IAlpha)c).Action(); // prints 'Alpha Action'
D. c.Action(); // prints 'Beta Action'

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    ((IAlpha)c).Action(); // prints 'Alpha Action' -> Option C
  4. Quick Check:

    Explicit interface methods need interface cast to call [OK]
Hint: Explicit interface methods require casting to interface type [OK]
Common Mistakes:
  • Calling explicit interface methods directly on class instance
  • Confusing explicit and implicit implementation
  • Assuming c.Action() works without cast