Interfaces help us define a set of rules that classes must follow. Implementing interfaces means a class promises to provide specific actions or behaviors.
Implementing interfaces in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
interface IExample { void DoWork(); } class MyClass : IExample { public void DoWork() { // method details here } }
Use the interface keyword to define an interface.
Use a colon : to show a class implements an interface.
IAnimal with a method Speak. The class Dog implements it by writing "Woof!".interface IAnimal { void Speak(); } class Dog : IAnimal { public void Speak() { Console.WriteLine("Woof!"); } }
IShape requires a method to get area. Circle implements it using its radius.interface IShape { double GetArea(); } class Circle : IShape { public double Radius { get; set; } public double GetArea() { return Math.PI * Radius * Radius; } }
This program defines an interface IVehicle with a method Drive. The class Car implements this method. In Main, we create a Car as an IVehicle and call Drive.
using System; interface IVehicle { void Drive(); } class Car : IVehicle { public void Drive() { Console.WriteLine("The car is driving."); } } class Program { static void Main() { IVehicle myCar = new Car(); myCar.Drive(); } }
All methods in an interface are public by default and cannot have bodies (unless using default interface methods in newer C# versions).
A class can implement multiple interfaces by separating them with commas.
Interfaces help with writing flexible and testable code.
Interfaces define what methods a class must have, without saying how.
Implementing an interface means writing those methods in your class.
This helps different classes work together in a clear way.
Practice
Solution
Step 1: Understand interface purpose
An interface declares methods without code, setting a contract.Step 2: Implementing means coding methods
A class that implements the interface must write the code for all those methods.Final Answer:
A class provides code for all methods declared in the interface. -> Option AQuick Check:
Implementing interface = writing required methods [OK]
- Thinking interfaces can create objects
- Believing interfaces inherit from classes
- Assuming methods are hidden, not implemented
IMyInterface in a class MyClass?Solution
Step 1: Recall C# interface syntax
In C#, a class implements an interface using a colon (:), not 'implements' or 'inherits'.Step 2: Check each option
class MyClass : IMyInterface { } uses correct syntax:class MyClass : IMyInterface { }. Others use wrong keywords or declare interface as class.Final Answer:
class MyClass : IMyInterface { } -> Option BQuick Check:
Use colon (:) to implement interface [OK]
- Using 'implements' keyword like Java
- Trying to inherit interface with 'inherits'
- Declaring interface as a class
interface IGreet { void SayHello(); }
class Person : IGreet {
public void SayHello() { Console.WriteLine("Hi!"); }
}
var p = new Person();
p.SayHello();Solution
Step 1: Understand interface and class
The interface IGreet requires SayHello method. Person implements it by printing "Hi!".Step 2: Trace code execution
Creating Person object and calling SayHello prints "Hi!" to console.Final Answer:
Hi! -> Option CQuick Check:
Implemented method runs and prints output [OK]
- Expecting method name printed instead of output
- Thinking interface prints output
- Assuming compile error without method body
interface IRun { void Run(); }
class Animal : IRun { }Solution
Step 1: Check interface requirements
IRun interface requires a method Run() to be implemented.Step 2: Verify class implementation
Animal class implements IRun but does not provide Run() method, causing error.Final Answer:
Class Animal must implement Run method. -> Option AQuick Check:
Implement all interface methods [OK]
- Forgetting to implement interface methods
- Thinking interfaces must have code
- Assuming empty class is allowed
interface IWalk { void Walk(); }
interface ITalk { void Talk(); }How can a class
Human implement both interfaces correctly?Solution
Step 1: Understand multiple interface implementation
A class can implement multiple interfaces by listing them separated by commas.Step 2: Provide all required methods
Human class must provide Walk() and Talk() methods with code.Final Answer:
class Human : IWalk, ITalk { public void Walk() { Console.WriteLine("Walking"); } public void Talk() { Console.WriteLine("Talking"); } } -> Option DQuick Check:
Multiple interfaces implemented with all methods [OK]
- Trying to declare multiple classes with same name
- Using 'implements' keyword (Java style)
- Declaring interface instead of class
