2

Possible Duplicate:
What does it mean to “program to an interface”?

First of all I think there are difference between the term interface and interface in java and I dont quite get what the difference is.

My first question when it says "Program to an interface rather than to an implementation" is that mean Interface or java Interface?

Im reading headfirst design patterns and I confused about program to an interface rather than program to an implementation. My understanding is that SUBCLASS IS THE CONCRETE IMPLEMENTATION and the INTERFACE is the SUPERCLASS(interface or abstract class or simpleclass).

My Second question is when they say program to an interface is that mean that the code is in the INTERFACE(superclass) then subclass just inherited it?? and if they say program to IMPLEMENTATION(subclass) the code is in the subclass?? I think about this because of the the term "ReUse", because if you change your code and your code is in the Subclass(concrete implementation) then you need to change all you subclass codes, then if you put your codes in the interface you just need to change the code in that interface.

also examples(please make it easy) of program to an interface and implementation will help.

3rd question what is the advantages if we program to an interface than if we program to concrete classes?

Community
  • 1
  • 1
user1348869
  • 103
  • 1
  • 4
  • In Java interfaces contain no code, only definitions of methods that some class must implement if they wish to group themselves with objects of that interface. – Hunter McMillen Apr 21 '12 at 22:05
  • 1
    Welcome to StackOverflow. Please take a minute or two to do a search of the site before posting your question. The subject line of your question contains an exact match to the duplicate @birryree links to, and should have been easy for you to find with a search. Keeping down duplicate questions helps keep SO a useful (and uncluttered) reference for programming questions. You might also take a few minutes to read the [FAQ](http://stackoverflow.com/faq) for more info that will help you to use this site more effectively. Thanks. :) – Ken White Apr 21 '12 at 22:13

1 Answers1

2

First of all I must say I have not read that book so I cannot say for sure this is what the author meant, but this is how I understand the sentence.

My understanding of what it means is that you should program in a way where you treat your classes and methods as black boxes, where you don't rely on some internal mechanisms and how they work, but you rather rely on the specifications you or someone else defined in the Interfaces, Superclasses, Documentation, etc. This way you can easily change the implementation without affecting the rest of your code base.

A nice example for this may be storing a sequence of some data, let's say names. You can define functions to add new names, delete them and print them. If you define these functions in an interface like the one below you can use them without having to bother whether the class that implements them uses arrays or lists to store them.

interface Sequence {
    void add(String s);
    void delete(String s);
    void print();
}

Now you can create a class SequenceAsList that implements the interface and use this in your application. Let's say everything works fine, but you now decided that maybe in some places you know the number of item in the sequnce in advance and you need a better performance and hence you would like to use array instead of list. So you can create another class SequnceAsArray that also implements the Sequence interface. Now everything you will need to change in your code will be to do

Sequence someSequence = new SequenceAsArray();

instead of

Sequence someSequence = new SequenceAsList();

Because they followed the same interface it's really easy to do this. Hence I think what the author mainly suggests is to take this approach of taking classes and methods as black boxes.

Laky
  • 965
  • 1
  • 6
  • 17