Originally posted by henri henri: I just learned that it is better programming practice if I change TreeMap at the beginning of the second line of code to Map.
Map<String, FilmBean>filmBeans = new TreeMap<String, FilmBean>();
after I do this, I get a compiler error for the return statement at the end of the method.
Well, you can't make the change and everything magically still works. You have to change your method to return a Map instead of a TreeMap. And you have to change all the places in your program to use a Map instead of a TreeMap.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
To sum up some previous responses, yes it's good programming practice to "program to the interface" when possible. In your example, it's more important for your getFilmBeansByLetter method to have return type Map (or Map<String FilmBean> in Java 1.5) that for its local value filmBeans to have type Map<String FilmBean> versus TreeMap<String FilmBean>, although the two can go hand-in-hand.