• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Generics

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>();
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 12
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.





 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:You do realize that your thread has nothing to do with generics?


++
reply
    Bookmark Topic Watch Topic
  • New Topic