0Pricing
C# Academy · Lesson

Interfaces (intro) & multiple interface implementation

Define interfaces as contracts, implement multiple interfaces in one class, use interface-based polymorphism, and learn explicit interface implementation basics.

Interfaces (intro) & multiple interface implementation is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Interfaces overview

Goal: Program to interfaces for flexible code.

  • Interfaces declare what, not how
  • Implement multiple interfaces
  • Pass interfaces to functions
  • Explicit implementation to resolve name clashes

Implementing multiple interfaces

Interfaces define a contract. One class can implement multiple interfaces to express different roles.

using System;

public interface IPlayable
{
  void Play();
}

public interface IHasDuration
{
  int DurationSeconds { get; }
}

public class Song : IPlayable, IHasDuration
{
  private readonly string _title;
  private readonly int _seconds;

  public Song(string title, int seconds)
  {
    _title = title;
    _seconds = seconds;
  }

  public int DurationSeconds { get { return _seconds; } }

  public void Play()
  {
    Console.WriteLine("Playing " + _title + " (" + _seconds + "s)");
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Song s = new Song("Hello", 5);
    s.Play();
    Console.WriteLine("Duration = " + s.DurationSeconds);
  }
}

Interface polymorphism

Accept an interface type in methods. Callers can pass any implementation.

using System;

public interface IPrintable
{
  void Print();
}

public class Report : IPrintable
{
  private readonly string _text;
  public Report(string text) { _text = text; }
  public void Print() { Console.WriteLine("Report: " + _text); }
}

public class Label : IPrintable
{
  private readonly string _text;
  public Label(string text) { _text = text; }
  public void Print() { Console.WriteLine("Label: " + _text); }
}

public class Program
{
  // Works with any IPrintable (polymorphism)
  static void PrintAll(IPrintable item)
  {
    item.Print();
  }

  public static void Main(string[] args)
  {
    PrintAll(new Report("Sales Q1"));
    PrintAll(new Label("Fragile"));
  }
}

Explicit implementation

Explicit implementation disambiguates same-named members and hides them from the class surface.

using System;

public interface ILogger { void Write(string msg); }
public interface IFileWriter { void Write(string msg); }

public class DualWriter : ILogger, IFileWriter
{
  // Explicit implementations: only visible via the interface reference
  void ILogger.Write(string msg) { Console.WriteLine("[LOG] " + msg); }
  void IFileWriter.Write(string msg) { Console.WriteLine("[FILE] " + msg); }

  public void Demo()
  {
    Console.WriteLine("DualWriter ready");
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    DualWriter dw = new DualWriter();
    dw.Demo();

    ILogger log = dw;
    IFileWriter file = dw;

    log.Write("started");
    file.Write("saved");
  }
}

Collections of interfaces

Store different implementations in a list of the interface type and iterate uniformly.

using System;
using System.Collections.Generic;

public interface ITask
{
  void Run();
}

public class EmailTask : ITask
{
  public void Run() { Console.WriteLine("Send email"); }
}

public class BackupTask : ITask
{
  public void Run() { Console.WriteLine("Run backup"); }
}

public class Program
{
  public static void Main(string[] args)
  {
    List<ITask> tasks = new List<ITask>();
    tasks.Add(new EmailTask());
    tasks.Add(new BackupTask());

    foreach (ITask t in tasks)
    {
      t.Run(); // calls the right implementation
    }
  }
}

Interface tips

Tips:

  • Prefer small interfaces that describe one role.
  • Use interfaces for testing (swap implementations).
  • In C# 6 interfaces have no fields or method bodies.
  • Implement multiple interfaces to compose behaviors.

Interfaces concept

Quick check: Which statement about interfaces in C# is correct?

Recap

Recap: Interfaces are contracts and support multiple implementation per class. Pass interfaces to functions, keep them small, and use explicit implementation to resolve conflicts.

Frequently asked questions

Is the “Interfaces (intro) & multiple interface implementation” lesson free?

Yes — the full text of “Interfaces (intro) & multiple interface implementation” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Interfaces (intro) & multiple interface implementation”?

Define interfaces as contracts, implement multiple interfaces in one class, use interface-based polymorphism, and learn explicit interface implementation basics. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Interfaces (intro) & multiple interface implementation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. virtual/override/sealed; abstract classes
  2. Interfaces (intro) & multiple interface implementation
  3. Polymorphism & dynamic dispatch
← Back to C# Academy