permaculture playing cards*
The moose likes Beginning Java and the fly likes Program to an interface, not an implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program to an interface, not an implementation" Watch "Program to an interface, not an implementation" New topic
Author

Program to an interface, not an implementation

Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
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

Joined: Aug 05, 2005
Posts: 3432
    
  12
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.


Joanne
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
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

Joined: May 03, 2004
Posts: 334
Please continue from here

i.e. The MainApp does not need to know about the actual object types.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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
 
subject: Program to an interface, not an implementation
 
Similar Threads
Program to an interface, not an implementation
Hunt Exam Question
List implementation
type Cast btn LIst and Vector, ArrayList
Quotes