• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics  RSS feed

 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason for declaring your collections as superclass objects? That is, "List" instead of "ArrayList"?

Example:
List<String> names = new ArrayList<String>();
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The principle of programming to an interface is really about managing dependencies.
If the declared type is an interface, you're bound to using that contract (useless you cast explicitly to its implementation) in your own code.
This way your code is decoupled from a concrete implementation, and you can switch implementations without breaking your code.
Thats a common practice in unit testing, for instance, when you swap out a dependency for a test double.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much of the time, you will not be writing all the code. You/your team will work on the GUI, and another team will work on the back end/database side, for example.

You say to the database team "we want a bunch of customers...how can we get them?".

The other team could say "call this method, with these parameters, and we will return <something>".

If the database team says "we will return an ArrayList", then you (and they) are stuck ALWAYS using an ArrayList. In six months, the DB team may come up with some brand new way to list the elements that is faster, smaller, better...but they can't give it to you because you are coded to expect an ArrayList. You could spend weeks re-coding to use a LinkedList, testing, debugging, and releasing, costing many thousands of dollars.

However, if they said "we will return a List", they can return an ArrayList, a LinkedList, or anything else...and you don't have to touch your code. It will just WORK, and you can reap the benefits for free.

Which sounds like it makes more sense from a business perspective?
 
Sandeep Gabhale
Greenhorn
Posts: 12
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To decouple your code from a specific implementation of the interface.
When you use
List<String> names = new ArrayList<String>();
the rest of your code knows that datatype is of type List<String>, which gives you flexibility to switch between different implementation of List interface.
Like, there may be a case when your required to use LinkedList<String> instead of ArrayList<String> in future for faster
insertion and deletion of data from List then at that time your code simply need the small change.
List<String> names = new LinkedList<String>();
and still rest of your code work as it is of datatype as List<String>.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any discussion about programming to interfaces versus concrete implementations is not complete without at least these two references:

1. Robert "Uncle Bob" Martin's articles on SOLID Design Principles - In particular, the DIP - Dependency Inversion Principle: depend on abstractions, not concretions

2. Joshua Bloch's book "Effective Java, 2nd Edition" - See Chapter 4 for a number of guidelines for effectively using Interfaces and Classes.

All the replies so far are related in some way to the writings of these luminaries of our profession cited above.
 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Programming to interface allows us to change the implementations with much less changes in code and provides decoupling. A good example is given at http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html.





 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
duhit Choudhary wrote:What is the reason for declaring your collections as superclass objects? That is, "List" instead of "ArrayList"?

You do realize that your thread has nothing to do with generics? I was tempted to change the subject for you, but perhaps it would be better if you did. I think you're confusing 'generics' with 'generalization'.

Other than that, I think Sandeep's post contains the best practical illustration for doing it.

Winston
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:You do realize that your thread has nothing to do with generics?

++
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!