I will go over to some of the basics of OO and try to understand why the use of interface reference variables exists.
When I create the interface:
public interface IWorker
{
int HoneySum { get; }
void getHoney();
}
and the class implements it:
public class Worker : Bee, IWorker
{
int honeySum = 15;
public int HoneySum { get { return honeySum; } }
public void getHoney()
{
Console.WriteLine("Worker Bee: I have this much honey: {0}", HoneySum);
}
}
why do people use:
IWorker worker = new Worker();
worker.getHoney();
instead of using:
Worker worker3 = new Worker();
worker3.getHoney();
What is the point of the interface reference variable if you can just set the class and use its methods and fields this way?
source
share