CodeBlanks Logo Icon CodeBlanks

Summary - Interfaces

C# course · Intermediate · Lesson 52/116

Combine multiple interfaces to write a payment system.

Summary - Interfaces

In this group you learned interfaces: an interface is a contract, a class implements it with :, multiple interfaces are comma-separated, default methods have a body and you saw the interface vs abstract distinction.

Interfaces unite different classes under a common contract and are the foundation of polymorphism. The next group dives into abstract classes: abstract methods, the virtual vs abstract distinction and abstract class hierarchies.

Exercise Printing interface (summary)

Define the IPrintable interface with the void Print(); method signature. Document and Main are already in place; you only add the interface.

Show example solution
Add `interface IPrintable { void Print(); }`.
Example solution:
using System;

interface IPrintable
{
    void Print();
}

class Document : IPrintable
{
    public void Print()
    {
        Console.WriteLine("Printed");
    }
}

class Program
{
    static void Main()
    {
        IPrintable d = new Document();
        d.Print();
    }
}

Exercise Logger (summary)

Define the FileLogger class: it implements ILogger. The public void Log(string message) method does Console.WriteLine("File: " + message);. In Main, create ILogger logger = new FileLogger(); and call logger.Log("Record").

Show example solution
ILogger { public void Log(string message) { Console.WriteLine("File: " + message); } }`. In Main write `ILogger logger = new FileLogger(); logger.Log("Record");`.
Example solution:
using System;

interface ILogger
{
    void Log(string message);
}

class FileLogger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine("File: " + message);
    }
}

class Program
{
    static void Main()
    {
        ILogger logger = new FileLogger();
        logger.Log("Record");
    }
}

Next step

Interfaces are a powerful tool for contracts and polymorphism. With default methods you preserve backward compatibility and with multiple interfaces you give classes different roles. The next group covers abstract classes: sharing common behavior, abstract methods and sealed constraints.

Remember: an interface says "can-do"; an abstract class says "is-a". Picking the right tool keeps the design clean.

Test yourself

  1. Fill in the blank: interface method call

    ILogger logger = new FileLogger();
    logger.____("Record");
    Show answer
    ILogger logger = new FileLogger();
    logger.Log("Record");
  2. Which statement about interfaces is correct?

    • An interface can be instantiated with new.
    • An interface can hold fields.
    • An implementing class must implement all the interface's methods.
    • An interface can be implemented by at most one class.
    Show answer

    An implementing class must implement all the interface's methods. — Correct. The implementing class must provide all the interface's methods.

All lessons in Intermediate