C# virtual interface

virtual method for interface implementation

To open the possibility for subclass to reimplement members from interface we can add virtual keyword to the implementation.


using System;//from   w  w w  . jav  a  2 s . c  o m
interface Printable{
    void print();
}

class Shape : Printable{
   public virtual void print(){
     Console.WriteLine("shape");
   }
}

class Rectangle : Shape{
   public override void print(){
     Console.WriteLine("rectangle");
   }
}


class Test
{
    static void Main()
    {

        Shape s = new Shape();
        s.print();

        Rectangle r = new Rectangle();
        r.print();

    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Inheritance
  2. Syntax for Class Inheritance
  3. Example for Class Inheritance
  4. Note for Class Inheritance
Home »
  C# Tutorial »
    C# Types »
      C# Interface
C# Interfaces
C# interface implementation
C# Explicit interface
C# Interface inheritance
C# Indexer in an interface
C# Interface Properties
C# struct and interface
C# virtual interface