Junior — Middle
Can you explain what is meant by the term 'interface' in programming?
sobes.tech AI
Answer from AI
In programming, an interface is an abstract type that defines a set of methods and properties but does not contain their implementation. The interface sets a contract that a class or structure must fulfill. This allows for creating flexible and extensible architectures where different classes can have a common set of functionalities implemented differently.
Example in C#:
interface ILogger
{
void Log(string message);
}
class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
Here, ILogger is an interface with a Log method. The ConsoleLogger class implements this interface, providing a specific implementation of the method.