Mega Code Archive

 
Categories / C# Tutorial / Class
 

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();    } }