TL ; DR:
An interface is a pure contract — a list of methods that a class promises to implement, with no shared data and no shared code. Unlike inheritance ("is a"), interfaces express "can do".
interface ISpeaker {
void Speak();
}
class Dog : ISpeaker {
public void Speak() {
Console.WriteLine("Woof");
}
}
Full lesson:
An interface defines what a class can do, not how it does it or what it is. A Truck, a Robot, and a Drone aren't the same kind of thing — but they can all Move(). You'd put Move() in an IMovable interface and have each one implement it.
Interface vs abstract class
| Abstract class | Interface | |
| Has fields | Yes | Usually no |
| Has method bodies | Yes (some) | No (or default impls only) |
| Multiple "inheritance" | Usually no | Yes — a class can implement many interfaces |
| Models | "is a" | "can do" |
A class inherits from at most one parent class but can implement many interfaces. That's why interfaces are the right tool for cross-cutting capabilities like IComparable, IDisposable, IEnumerable, etc.
In C#
Interfaces use the interface keyword, and by convention their names start with I. C# uses : both for inheriting from a class and for implementing interfaces; a class lists its base class first, then any interfaces.
Your task
The ISpeaker interface is already declared. Create a Dog class that implements it, with a Speak() method that prints "Woof".
Expected output:
WoofFree plan: 30 tutor messages. PRO is unlimited.
