A co-worker came to me with a question and it made me think of
this recent
thread, although this one didn't quite hit on the exact question I have.
What is the usefulness of writing this:
I understand the use of interface references when it comes to method parameters and in the realm of
polymorphism, but, when you're creating the object, I don't see what the point of storing it in an interface reference is. Any time you try to use that object in a place where an interface reference is expected, the object will simply be implicitly casted by the compiler. Take the following example:
In this example, both calls to foo will compile just fine and, in the second case, the ArrayList will be implicitly cast into a List so that it can be used in the method foo. What I'm really getting at is why would you ever want to use the first line rather than the second line?
Let's look at another example. Assume you have a factoy which can create an object that implements FactoryObject. Then, I see the use of being able to assign to an interface reference type.
However, in this case, we're not using new to create an object - the Factory class is doing that for us. Let's look inside the factory class, however:
There's no reason to assign the object that was really created, or cast it as a FactoryObject. The compiler will do that for you. Granted, the previous method could be rewritten like this, in order to follow a more "accepted" programming style.
This is a case where using an assignment to an interface reference is useful, but only for greater readability. It is prefectly legal to bypass the assignment, as shown above.
So, now that I've shown a case where assigning an object to an interface reference was "pseudo-useful," let's look at a case where it might be detrimental.
If a (non abstract) class implements an interface, you are guaranteed that the class is question will implement all of the methods of that interface. However, you are not guaranteed that the class in question won't provide extra functionality, like this:
Now, assuming that this is the case, take a look at this code, which tried to use an object of type InterfaceAImplementor:
As you can see, after assigning the object to an interface reference type, we've "narrowed" our view into the object and what it can do. In order to get access to the methods that we need to call, we'd have to case it as the original object first.
So, what I'm really getting at is that a lot of people seem to think that
you should assign an object to an interface reference rather than to a reference to the class the object is a type of whenever possible. I don't understand why, in general, this is a good practice. If anything, I find it more detrimental, than beneficial.
Please
do not misinterpret this question. I'm not asking what the usefulness of interfaces is - I'm well aware of how useful they can be. I'm asking, specifically, is it really a good practice to assign an object to an interface reference, is possible. What happens if an object implements multiple interfaces? Then what do you assign it to?
Thanks,
Corey