CSharp - Reimplementing an Interface in a Subclass


A subclass can reimplement any interface member already implemented by a base class.

In the following example, PDFDocument implements Printable.Print explicitly, and so it cannot be marked as virtual.

In order to "override" it, RichPDFDocument must reimplement Printable's Print method:

interface Printable { void Print(); }

class PDFDocument : Printable
       void Printable.Print() => Console.WriteLine ("PDFDocument.Print");

class RichPDFDocument : PDFDocument, Printable
       public void Print() => Console.WriteLine ("RichPDFDocument.Print");

Calling the reimplemented member through the interface calls the subclass's implementation:

RichPDFDocument r = new RichPDFDocument();
r.Print();                 // RichPDFDocument.Print      Case 1
((Printable)r).Print();    // RichPDFDocument.Print      Case 2