Why do we use interface references to call methods of an object?
For example:
class Program : Iabc, Idef
{
static void Main(string[] args)
{
Console.WriteLine("Hello interfaces");
Program refProgram = new Program();
Iabc refiabc = refProgram;
refiabc.xyz();
Idef refidef = refProgram;
refidef.pqr();
Console.ReadLine();
}
public void pqr()
{
Console.WriteLine("In pqr");
}
public void xyz()
{
Console.WriteLine("In xyz");
}
}
The interface reference refiabc is pointing to the refProgram reference which is pointing to the Program object. refiabc then calls xyz()
Why do we do this (use interface references that point to a central reference that points to an object) instead of calling methods directly by making an instance of a class then calling its methods?
Example:
Why do this
Program refProgram = new Program();
Iabc refiabc = refProgram;
refiabc.xyz();
Instead of just doing this
Program refProgram = new Program();
refProgram.xyz();