last day (15 days later) » 

18:51
247
A: What does it mean to "program to an interface"?

Bill the LizardThe specific example I used to give to students is that they should write List myList = new ArrayList(); // programming to the List interface instead of ArrayList myList = new ArrayList(); // this is bad These look exactly the same in a short program, but if you go on to use myList 100 time...

In Java it's an interface. But you have the idea, you want whatever the interface is, not a specific implementation.
@EdS. Most answers to this question have the common misconception that "programming to an interface" means use the interface language construct; which is totally wrong! This is the first answer I've seen that correctly illustrates that "programming to an interface" means: don't unnecessarily bind your 'client code' to concrete/specific subclass implementations because if you later decide to change it use a different implementation, you have a lot more work undoing all the unnecessary bindings. I.e. program to/bind to things without implementation details. E.g. Abstract base classes.;)
+1 to @CraigYoung comment. Inheritance is intended to enable one to express all sub-classes in the same way. This is the fundamental description of an interface.
I totally think THIS COMMENT is the RIGHT ANSWER to the question. How to group different objects by using interface (as explained by Peter Meyer above) is a basic thing. The key concept to answer the title question is the interchangeability of the classes implementing the interface, making changes to your code easier. The lower the level of coupling between your modules/classes is, the easier it is to make changes. It's to bad those people who just go for the top-voted answer and think they understood, will never have the chance to realize, they didn't get an answer they were looking for.
Genial text regarding Interfaces by Peter Meyer. Short but very helpful ANSWER by Bill the Lizard :)
18:51
In what case would you want to change myList to a TreeList implementation later on instead of just using a different variable to represent a TreeList?
@ZinanXing When you only need one list, but you want to change it from an ArrayList to a TreeList.
When you said no ArrayList specific methods, does that mean I cannot use any methods in the ArrayList class??? I was facing problem for peek() method on LinkedList.
@SreecharanDesabattula You'd only be able to use methods from the List interface if you declare a variable like List a = new ArrayList();. Even though the object is an ArrayList, the reference that points to it is a List reference.
@BilltheLizard sorry, but when I'm reading your answer now...I want to ask one question here: What if some X class is implementing the I interface and want to add some n methods of its own...Will the reference of interface not restrict the access to that n methods? i mean, what to do in this kind of situation?
@krupalshah Yes, the Interface reference will restrict access and you will not be able to call the new methods implemented only in the class. In that case you need a reference that's the same type as the object.
18:51
@BilltheLizard ok, I understood. Thank you so much for your time and great explanation in the answer.
@CraigYoung: A huge thanks! I am so glad to see others calling out the misunderstanding about what this important principle means. There are a lot of people out there and, sadly, other answers here in SO, that don't understand it. Indeed, it's not about creating separate interfaces for everything, but about using the interfaces you already have in the client code. Hopefully, more people will get to understand the principle and stop creating so many pointless, unnecessary interfaces.
This is really the only answer that gave me the "aha" moment on why one should use Drawable square = new Square() over Square square = new Square() (when applicable).
I honestly never saw any point in restricting the type to an interface. It just means you can't use implementation-specific functions. Unless that List comes from outside and you're not sure what type of List it is, it's just not useful. If you put so little thought in your initial design you need to swap it out later, you fully deserve the extra work anyway.
@Nyerguds Spoken like someone who has never had to work on a project longer than it takes to submit it for a grade.
@BilltheLizard Nah, I've been programming professionally for half a decade now, and I've had to do some serious coding archaeology. It's in those things that I've seen code like List lst = new ArrayList();, followed by bizarre workarounds or even home-written functions that end up doing nothing but trying to replicate functionality that is already available in ArrayList class. It's a bad practice propagated by people who don't have a clue what they're doing, and no one ever bothers to explain to people why it's being taught in schools that way. Because there is no good explanation.
18:51
@Nyerguds You've identified a problem but misattributed it. What you describe is equivalent to typecasting an object to a more specific type. (That's a big red flag in OO design.) If someone is hacking through an abstraction to access implementation specific methods, then it means they chose the wrong abstraction. (It doesn't mean they shouldn't bind to an abstraction.)
@BilltheLizard Honestly, on this it is indeed a fairly trivial difference since ArrayList is a pretty straightforward implementation of List. But from the moment the implementing class has more actually useful functions (and most of the times, they do), this starts going all wrong, and I've never had a teacher give me a decent reason for restricting yourself in this. Especially on private internally used variables, it's.just cutting off your own fingers.
But what if I want to use ArrayList specific methods?
@MáraToner Then use an ArrayList.
I find this to be quite frankly wrong. The real answer is to use the public methods of the ArrayList and not worry about how it is implemented. This is NOT a Java thing. I find it quite ridiculous that you would instantiate an ArrayList in this way as you remove the useful features of an ArrayList, or any other implementation that you might use. The methods of a List interface are still available in an ArrayList. But this whole Java based explanation of code to the interface is just WRONG. And here you are crippling your code because you think it might help some unknown future issue.
@BillR I said one comment above yours that if you need ArrayList methods, use an ArrayList.
18:51
It doesn't change the fact that in my opinion your answer is wrong and doubly so for using a Java based reason that also doesn't hold water (because it hamstrings the code for no real benefit). I have a fleet of specialty cars that as a base can all do the same thing as a Ford Fiesta. But rather than take advantage of the benefits of the different cars' specialties, I'll just tell the drivers to drive them all like they are Ford Fiestas.
@BillR If all of those cars have steering wheels and gas and brake pedals, they do have the same API as a Ford Fiesta. Thanks for making my point for me.
On the other hand, though: if you program to the LIst interface you may find yourself doing something less-than-ideal like repeatedly accessing something in a LinkedList by index. I suppose this is more the fault of the interface spec than the concept though.
@BilltheLizard great explanation.

last day (15 days later) »