Junior — Middle
Can you explain what an interface in programming is?
sobes.tech AI
Answer from AI
An interface in programming is an abstract type that defines a set of methods (method signatures) without their implementation. It sets a contract that classes implementing this interface must fulfill.
In C#, an interface is declared using the interface keyword. A class that implements an interface is required to implement all its methods.
Example:
public interface ILogger
{
void Log(string message);
}
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
Interfaces allow for creating flexible and extensible architectures, provide polymorphism, and facilitate testing.