Home arrow static arrow Java Programming [Archive] - "program to the interface"
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - "program to the interface"
This topic has 19 replies on 2 pages.    1 | 2 | Next »

Posts:346
Registered: 1/15/04
"program to the interface"  
Jul 13, 2004 5:38 AM



 
I have heard this war cry several times in the last while.
can anyone explain what it means?
 

Posts:10
Registered: 6/3/04
Re: "program to the interface"  
Jul 13, 2004 5:41 AM (reply 1 of 19)



 
You need to be more clear in what you want.
 

Posts:24,036
Registered: 2/3/03
Re: "program to the interface"  
Jul 13, 2004 5:55 AM (reply 2 of 19)



 
It's a means of programming by which your applications tend remain more flexible/modular. The underlying implementations can be changed without affecting the overall design.

[url=http://java.sun.com/docs/books/tutorial/java/concepts/]Object-Oriented Programming Concepts[/url]
[url=http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-java101.html]Object-oriented language basics[/url]
[url=http://sepwww.stanford.edu/sep/josman/oop/oop1.htm]Don't Fear the OOP[/url]
[url=http://mindprod.com/jgloss/interfacevsabstract.html]Java Glossary : interface vs abstract class[/url]
[url=http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html]Abstract classes vs. interfaces: When does it make sense to choose an abstract class over an interface?[/url]
[url=http://www.javaworld.com/javaworld/javaqa/2001-08/03-qa-0831-interface.html]Abstract classes and interfaces practicum: Move from theory to practice on when to employ abstract classes vs. interfaces[/url]
[url=http://java.sun.com/developer/JDCTechTips/2001/tt1106.html#tip2]Tech Tips: ABSTRACT CLASSES VS. INTERFACES[/url]
 

Posts:24,036
Registered: 2/3/03
Re: "program to the interface"  
Jul 13, 2004 5:56 AM (reply 3 of 19)



 
Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url]
Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]
 

Posts:441
Registered: 2/25/04
Re: "program to the interface"  
Jul 13, 2004 5:57 AM (reply 4 of 19)



 
It's a weak form of [url=http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple]LSP[/url]

Pete
 

Posts:7,258
Registered: 1/31/02
Re: "program to the interface"  
Jul 13, 2004 6:04 AM (reply 5 of 19)



 
I have heard this war cry several times in the last
while.
can anyone explain what it means?

were you , perchance, spending some time here:

http://forum.java.sun.com/thread.jsp?forum=425&thread=529847

?
 

Posts:346
Registered: 1/15/04
Re: "program to the interface"  
Jul 13, 2004 8:51 AM (reply 6 of 19)



 
So if I have a method which returns a Vector, I read that I should have a return type of List as this is more flexible.
But why not have a return type of Cloneable, Collection, RandomAccess or Serializable as Vector implements each of these interfaces or do I base the return type on the API methods I am using?
 

Posts:24,036
Registered: 2/3/03
Re: "program to the interface"  
Jul 13, 2004 8:52 AM (reply 7 of 19)



 
or do I base the return type on the API methods I am using?

Bingo, baby.
 

Posts:6,750
Registered: 1/25/04
Re: "program to the interface"  
Jul 13, 2004 9:18 AM (reply 8 of 19)



 
IMO the way to think about it is not "I have a Vector, what should I return" but "what will the calling code require?" If the place where this thing is going to go needs something that contains a bunch of items in a particular order, then List satisfies that requirement. Then you can decide what kind of List to use. If it needs something it can write to disk, sounds like a Serializable would fit the bill. And so on.
 

Posts:4,906
Registered: 23/07/02
Re: "program to the interface"  
Jul 14, 2004 7:29 AM (reply 9 of 19)



 
Although I approve of the advice given so far, I would understand "program to the interface" not in terms of interfaces vs classes but rather to mean that you shouldn't rely on undocumented behaviour. For example, you shouldn't depend on the existence of private fields of classes in the standard libraries (as it is possible to depend when using reflection).
 

Posts:1,657
Registered: 1/8/98
Re: "program to the interface"  
Jul 14, 2004 9:08 PM (reply 10 of 19)



 
nasch_ hit it pretty much on the head, except for one nit-pick: I'd never use Serializable as a return type - it doesn't do anything (being a marker interface), so that kind of return is pretty useless. As a method parameter, on the other hand, it's very handy, especially when handing off to other systems, like JMS wrappers might - kind of like you don't really care what the specific object coming into the method is, but it had better implement Serializable because it's ultimately going out "over the wire."
 

Posts:6,750
Registered: 1/25/04
Re: "program to the interface"  
Jul 15, 2004 8:18 AM (reply 11 of 19)



 
Sounds reasonable. YATArchivist's comments are good, too. I think both principles are important, though what to call each one I'm not sure.
 

Posts:3,258
Registered: 00-08-28
Re: "program to the interface"  
Jul 15, 2004 8:40 AM (reply 12 of 19)



 
[url http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html] why extends is evil[/url]
 

Posts:6,750
Registered: 1/25/04
Re: "program to the interface"  
Jul 15, 2004 9:03 AM (reply 13 of 19)



 
Well, that was random. Thanks, kilyas. :-) I happen to disagree with both of his "Why ... is evil" articles, but they're a valuable perspective.
 

Posts:3,258
Registered: 00-08-28
Re: "program to the interface"  
Jul 15, 2004 9:07 AM (reply 14 of 19)



 
Well, that was random. Thanks, kilyas. :-) I happen
to disagree with both of his "Why ... is evil"
articles, but they're a valuable perspective.

Well I donot fully agree with his getter/setter ..... evil article because I consider it a matter of option and not choice. But I would put more weight in this one.
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »