Interface Basics
In this page:
Interface Basics
An interface declares a contract of members that implementing classes must provide, but supplies no implementation of its own (aside from default methods). It's declared with interface and conventionally named starting with I, like IShape. Interfaces define *what* a type can do, not *how* it does it.
Note: Interface members are implicitly public — you never write an access modifier on them.
Example: Interface Basics
using System;
interface IGreeter
{
void Greet();
}
class EnglishGreeter : IGreeter
{
public void Greet()
{
Console.WriteLine("Hello!");
}
}
class Program
{
static void Main(string[] args)
{
IGreeter g = new EnglishGreeter();
g.Greet();
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: