Reference an object by interface and class : Object Reference : Class C# Examples


C# Examples » Class » Object Reference »

 

Reference an object by interface and class






You can create an interface reference variable.
Such a variable can refer to any object that implements its interface.
When you call a method on an object through an interface reference, the method is the version of the method implemented by the object.





    
public  interface  Player
{
  void  PlayMusic();
}

public  class  Student  :  Player
{
  public  void  PlayMusic(){}
  public  void  DoALittleDance(){}
}

public  class  MainClass
{
      static  void  Main()
      {
            Student  st  =  new  Student();
            Player  musician  =  st;

            musician.PlayMusic();
            st.PlayMusic();
            st.DoALittleDance();
      }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Object Reference