File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Program to an interface, not an implementation

 
Praveen Kumar
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,


Could any one explain the Program to an interface, not an implementation ?

I have read the articles on this.. but still .....


Thanks In Advance.
Praveen
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it you don't understand ? Most people will just post something similar to what is in the articles you have read. If you tell us which particular part you don't understand, you will likely get a more useful answer.
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this link should help you understand interfaces and when to use them better. http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

Justin Fox
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program to an Interface not to an Implementation means program to a supertype. It means, the declared type of the variable should be a supertype usually an abstract class or Interface. The object assigned to the variable can be any of the concrete implementation of the supertype. Check the following example given below.


The class [b] MainApp does not need to know
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please continue from here

i.e. The MainApp does not need to know about the actual object types.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try, though it does tend to cause confusion when several people reply simultaneously.

You declare in terms of the superclass (or better in terms of the interface) and when you instantiate the subclass you can be sure of a certain set of methods available. You use that set, which are the same as the interface.

If you wish to change the implementation you can alter the instantiated type.

You can change that last bit to new LinkedList<String>() secure in the knowledge the program will still work.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic