tags:

views:

39

answers:

6

I am going over some OO basics and trying to understand why is there a use of Interface reference variables.

When I create an interface:

public interface IWorker
    {
        int HoneySum { get; }

        void getHoney();
    }

and have a class implement 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 just using:

Worker worker3 = new Worker();
            worker3.getHoney();

whats the point of a interface reference variable when you can just instatiate the class and use it's methods and fields that way?

+1  A: 

You want to keep it as generic as possible without tying to specific implementation.

fastcodejava
A: 

Basically it's considered good programming practice to use the interface as the type. This allows different implementations of the interface to be used without effecting the code. I.e. if the object being assigned was passed in then you can pass in anything that implements the interface without effecting the class. However if you use the concrete class then you can only passin objects of that type.

There is a programming principle I cannot remember the name of at this time that applies to this.

Derek Clarkson
+3  A: 

If your code knows what class will be used, you are right, there is no point in having an interface type variable. Like in your example. That code knows that the class that will be instantiated is Worker, because that code won't magically change and instantiate anything else than Worker. In that sense, your code is coupled with the definition and use of Worker.

But you might want to write some code that works without knowing the class type. Take for example the following method:

public void stopWorker(IWorker worker) {
    worker.stop(); // Suppose that IWorker has a method stop()
}

That method doesn't care about the specific class. And would handle anything that implements IWorker.

That is code you don't have to change if you want later to use a different IWorker implementation.

It's all about low coupling between your pieces of code. It's all about maintainability.

Sergio Acosta
A: 

Interfaces are used to achieve loose coupling between system components. You're not restricting your system to the specific concrete IWorker instance. Instead, you're allowing the consumer to specify which concrete implementation of IWorker they'd like to use. What you get out of it is loosely dependent components and better flexibility.

Sergey
A: 

One major reason is to provide compatibility with existing code. If you have existing code that knows how to manipulate objects via some particular interface, you can instantly make your new code compatible with that existing code by implementing that interface.

This kind of capability becomes particularly important for long-term maintenance. You already have an existing framework, and you typically want to minimize changes to other code to fit your new code into the framework. At least in the ideal case, you do this by writing your new code to implement some number of existing interfaces. As soon as you do, the existing code that knows how to manipulate objects via those interfaces can automatically work with your new class just as well as it could with the ones for which it was originally designed.

Jerry Coffin
A: 

Think about interfaces as protocols and not classes i.e. does this object implement this protocol as distinct from being a protocol? For example can my number object be serialisable? Its class is a number but it might implement an interface that describes generally how it can be serialised.

A given class of object may actually implement many interfaces.

Christopher Hunt