Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes OO, Patterns, UML and Refactoring and the fly likes Programming to an intreface? 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 » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Programming to an intreface?" Watch "Programming to an intreface?" New topic
Author

Programming to an intreface?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Guys,

Can anyone please explain me with a scenario about what it means by saying programming to an interface rather than implementation?

Thanks in advance!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
An example from using the Collections framework would be that instead of a line like

ArrayList myList = new ArrayList()

you would use

List myList = new ArrayList()

This has the benefit that if you later decide that ArrayList should be changed to some other class that implements List, you only need to change a single line of code. This is especially important in public APIs. You should not have an API method like

public ArrayList myMethod (HashMap myMap) { ... }

but instead

public List myMethod (Map myMap) { ... }


Ping & DNS - my free Android networking tools app
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Ulf,

So that means we always have to use the supertype reference and dynamically decide on the object. Am I right???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Certainly not always. If you find that the difference between, say, List and ArrayList matters to your code -because you're using ArrayList methods that are not part of List- then that's an indicator for using it as ArrayList, and not List. Otherwise, plenty of instanceof calls and type casts pile up quickly. If all uses of a particular type are within the same class (and certainly if they are within the same method), then using the more specific type probably makes sense. But there are no hard rules, so it's always a bit of a judgement call that depends on the circumstances.
Mike Isano
Ranch Hand

Joined: Jan 19, 2007
Posts: 144
I think it means using polymorphism.



I don't like the ArrayList example.

If we have something like this:

public class Monster
{
Person[] personArray;

public void killEveryone(Weapon w)
{
//for each person in the array
for(Person p : this.PersonArray)
{
w.attack(p);
}
}

}//end of class


The weapon in the killEveryone method is not a specific weapon like "sword" or "gun". It's just a theoretical weapon. No matter what kind of specific weapon you pass in, the killEveryone method will work.

Weapon could be an interface or a class.

The advantage of this is that the code of actually killing someone could be very different. If you attack someone with a gun you need to display special graphics of bullets flying, etc. When you attack someone with a sword the distance between the monster and the person needs to be calculated to make sure it's in melee range, etc. These differences no longer matter becuase your only interested in the high level concept that you are attacking people with a weapon. Changes in the specific weapon classes don't effect the high level algorithm.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Ulf Dittmer:
Certainly not always. If you find that the difference between, say, List and ArrayList matters to your code -because you're using ArrayList methods that are not part of List- then that's an indicator for using it as ArrayList, and not List. Otherwise, plenty of instanceof calls and type casts pile up quickly. If all uses of a particular type are within the same class (and certainly if they are within the same method), then using the more specific type probably makes sense. But there are no hard rules, so it's always a bit of a judgement call that depends on the circumstances.


I use that rule too. ArrayList within a class, but once it leaves the class its List all the way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Programming to an intreface?
 
Similar Threads
new forum - testing
Tech Word Game
WA #1.....word association
Tools for testing static methods
Pair programming