Loading...

C# Interfaces

Name a method on an interface, then fill that method in a class. Write public interface Signal with void Horn(); and no body. Write public class Truck : Signal and give Horn a public body that prints honk. Inside Pine.Main, build a Truck and call t.Horn(). Run with dotnet run. The screen should show honk. A second demo lists two interfaces after the colon, Signal and Mark, and the same class fills both. Leave enums for the next page. This lesson is a promise with no body, and the class that keeps it.

Fill one interface method

The overriding page replaced Horn on a child that already had Truck : Car, using virtual and override. This page starts from a promise that has no body at all. Open the Pine file. The starting class name stays Pine. Delete leftover Car and Truck pairs if they bury the interface story, leftover constructor prints of car and truck, leftover Path demos, and any ReadLine line. The program must not wait for typing. Keep using System; at the top.
Declare the interface above the classes:
public interface Signal
Inside it, name the method and stop. A semicolon ends the line. There are no braces and no print:
void Horn();
Do not write public on that interface line. An interface method is already public. Do not write virtual or override on it either. The interface only names the call.
Declare the class that keeps the promise. The colon here lists an interface, not a parent class:
public class Truck : Signal
Inside Truck, write a public method with the same name and fill the body:
public void Horn()
Console.WriteLine("honk");
The class must provide a body for every method named on the interface. On this page that is only Horn. The class method must say public. It must not say override. Nothing on Signal is a method body to replace, so override has no parent body to point at.
Inside Main, build a truck and call the method:
Truck t = new Truck();
t.Horn();
The call runs the body written on Truck, so the screen prints honk.

Pine Truck : Signal Horn honk

csharp
using System;

public interface Signal
{
    void Horn();
}

public class Truck : Signal
{
    public void Horn()
    {
        Console.WriteLine("honk");
    }
}

public class Pine
{
    public static void Main()
    {
        Truck t = new Truck();
        t.Horn();
    }
}

Save, then run with dotnet run. The screen shows honk. Walk the run once. C# starts at Pine.Main. new Truck() builds a truck whose class promised Signal. t.Horn() runs the body on Truck and prints honk. You never write new Signal(). An interface names methods. It does not build objects. The class supplies the bodies, and new builds the object from the class.
If the build refuses, check four things. The class line must say Truck : Signal. The word implements is Java, and C# does not know it. Every interface method needs a matching method on the class. The method on the class must be public. And that class method must not be marked override, because there is no virtual method above it to replace. If you still see beep from an old override file, switch to the interface file and run again.
Say the rule out loud while you watch the screen: the interface names the call, the class fills the body, the object runs that body. Keep the colon on the class that owns the print. Do not invent an enum on this page. Enums wait for the next lesson.

List two interfaces on one class

A second program keeps Pine and Truck. Now Truck lists two interfaces. Signal still asks for Horn. Mark asks for Show. The class fills both bodies. Main calls both methods.

Pine Truck : Signal, Mark

csharp
using System;

public interface Signal
{
    void Horn();
}

public interface Mark
{
    void Show();
}

public class Truck : Signal, Mark
{
    public void Horn()
    {
        Console.WriteLine("honk");
    }

    public void Show()
    {
        Console.WriteLine("truck");
    }
}

public class Pine
{
    public static void Main()
    {
        Truck t = new Truck();
        t.Horn();
        t.Show();
    }
}

Run again with dotnet run. The screen shows honk, then truck. The first line comes from the Signal promise filled on Truck. The second line comes from the Mark promise filled on the same class. Separate the interface names with a comma after the colon: Truck : Signal, Mark.
Read the class header once more. One class. Two interfaces. Two method bodies. That is the beginner picture of keeping more than one promise. A C# class can list only one parent class, and that parent must come first if you ever add one. It can list several interfaces after the colon. This page has no parent class, so both names after the colon are interfaces.
If you leave out Show while Mark is listed, the build refuses, because every listed interface method needs a body. If you leave Mark off the class line but still write Show, you have an ordinary method. The compiler is not checking it against Mark. Keep both names on the class line when you want both promises checked. Keep both methods public.

Compared with override

On the overriding page, Truck : Car replaced a Horn that already had a parent body, and the child method needed override. On this page, Signal starts with no body, and Truck : Signal supplies the first body, with no override word. You still run with dotnet run. You still call t.Horn(). The new pieces are interface, the colon that lists interfaces, and, in the second demo, a comma between two interface names.
The colon has worn three hats in this module. On a class, Truck : Car named a parent class. On a constructor, Truck() : base() named a parent constructor. On this page, Truck : Signal names an interface. Same punctuation, different neighbor. Read the name after the colon before you decide which hat it is wearing.
If a leftover override file is still the one you run, you will see virtual, override, and maybe beep beside honk. Switch to the interface file so the top of the file shows public interface Signal and the class line shows : Signal with no Car. Keep enum demos off this page. Keep keyboard input off this page.

Mistakes that look like the previous pages

Writing implements Signal copies Java and does not build. Writing override on the class Horn copies the previous page and does not build, because Signal never marked a method virtual. Leaving off public on the class method hides it, and a hidden method cannot keep a public promise, so the build says Truck does not implement Signal. Calling new Signal() asks C# to build an object from a type that has no body. Each of those mistakes has a clear build message. The teaching file avoids all four.

What this leaves for enums

This page fills interface methods with a class after a colon. The next page shows enums, where a type lists a fixed set of named values. Stay with interface promises here. Do not replace today's demos with an enum list as a requirement yet.
When you later use enums, you may still implement interfaces on other types in the same program. Treat this page as the interface half of that neighboring story. Run both Pine demos until honk, and then honk plus truck, feel automatic before you open the enums lesson.

Before enums

You can write public interface Signal with an empty Horn, list it on Truck, and see honk from t.Horn(). You can list Signal and Mark on one class and see honk, then truck. Run Pine with dotnet run for each kept demo in turn. The class kept the interface promises. The next page is enums, where a type lists fixed names.
Keep the single-interface honk demo for a clear colon story. Keep the two-interface demo for a clear comma list. Keep enum files off this page. Those three habits are the checkpoint before you move on. When honk appears after t.Horn(), and when the second demo also prints truck after Show, interfaces did their job for this lesson, and you are ready for the next topic about enums.
One last pass through the file: one Pine with Main, interface headers with empty methods, a class that lists them after a colon, public method bodies, no override, no keyboard wait, and no forced enum. Run dotnet run once more on the demo you keep open. If the screen still prints the fills you wrote, the promises are solid.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top