create an instance of a class that pointed to by interface of that class?

Discussion in 'Java' started by Matt, Apr 6, 2004.

  1. Matt

    Matt Guest

    In the following code, I need the best practice is (1), but I still
    don't understand why. So the rule of thumb is if possible, it is best
    to create an instance of a class that pointed to by interface of that
    class?

    HashMap m = new HashMap(); --(1)
    Map m = new HashMap(); -- (2)

    Please advise. Thanks!
     
    Matt, Apr 6, 2004
    #1
    1. Advertising

  2. Matt

    Tony Morris Guest

    "Matt" <> wrote in message
    news:...
    > In the following code, I need the best practice is (1), but I still
    > don't understand why. So the rule of thumb is if possible, it is best
    > to create an instance of a class that pointed to by interface of that
    > class?
    >
    > HashMap m = new HashMap(); --(1)
    > Map m = new HashMap(); -- (2)
    >
    > Please advise. Thanks!


    http://java.sun.com/docs/books/tutorial/collections/index.html

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Apr 6, 2004
    #2
    1. Advertising

  3. Matt

    Roedy Green Guest

    On 5 Apr 2004 18:23:17 -0700, (Matt) wrote or
    quoted :

    >HashMap m = new HashMap(); --(1)
    >Map m = new HashMap(); -- (2)


    Both have their uses.

    If you plan only on using the Map methods of m, use Map, since that
    style will block you from inadvertently using some methods peculiar to
    HashMap. It will then me easier to substitute a different Map
    collection just by changing the word after new. Nothing else need
    change.

    If you want specifically a HashMap, then the first form is preferable.
    It is more direct, faster, and allows you all ALL the methods of
    HashMap.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 6, 2004
    #3
  4. Matt

    Tony Morris Guest

    "Roedy Green" <> wrote in message
    news:...
    > On 5 Apr 2004 18:23:17 -0700, (Matt) wrote or
    > quoted :
    >
    > >HashMap m = new HashMap(); --(1)
    > >Map m = new HashMap(); -- (2)

    >
    > Both have their uses.
    >
    > If you plan only on using the Map methods of m, use Map, since that
    > style will block you from inadvertently using some methods peculiar to
    > HashMap. It will then me easier to substitute a different Map
    > collection just by changing the word after new. Nothing else need
    > change.
    >
    > If you want specifically a HashMap, then the first form is preferable.
    > It is more direct, faster, and allows you all ALL the methods of
    > HashMap.
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > Coaching, problem solving, economical contract programming.
    > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


    HashMap has no additional methods than what Map provides.
    The performance difference is negligible (about an extra 10% on most
    benchmarks), and is implementation-dependant.

    I'd go so far as to say that there is never a good reason to declare a
    HashMap reference (or any of the Collections types that *shouldn't* have
    references declared)

    http://java.sun.com/docs/books/tutorial/collections/index.html.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Apr 6, 2004
    #4
  5. Matt

    Roedy Green Guest

    On Tue, 6 Apr 2004 15:44:01 +1000, "Tony Morris"
    <> wrote or quoted :

    >
    >HashMap has no additional methods than what Map provides.


    I think he was asking a more general question, and in general the
    specific class does have some extra methods the interface does not.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 6, 2004
    #5
  6. Tony Morris wrote:

    >
    > "Roedy Green" <> wrote in message
    > news:...
    >> On 5 Apr 2004 18:23:17 -0700, (Matt) wrote or
    >> quoted :
    >>
    >> >HashMap m = new HashMap(); --(1)
    >> >Map m = new HashMap(); -- (2)

    >>
    >> Both have their uses.
    >>
    >> If you plan only on using the Map methods of m, use Map, since that
    >> style will block you from inadvertently using some methods peculiar to
    >> HashMap. It will then me easier to substitute a different Map
    >> collection just by changing the word after new. Nothing else need
    >> change.
    >>
    >> If you want specifically a HashMap, then the first form is preferable.
    >> It is more direct, faster, and allows you all ALL the methods of
    >> HashMap.
    >>

    >
    > HashMap has no additional methods than what Map provides.
    > The performance difference is negligible (about an extra 10% on most
    > benchmarks), and is implementation-dependant.
    >
    > I'd go so far as to say that there is never a good reason to declare a
    > HashMap reference (or any of the Collections types that *shouldn't* have
    > references declared)
    >
    > http://java.sun.com/docs/books/tutorial/collections/index.html.
    >


    I tend to use the first style when declaring method local variables. If
    oneday I need to change the Map implementation (I don't think I've ever had
    to :), it won't be much work anyway.

    When it comes to the public interface of a class though, I always use the
    interface type instead, which is just common sense.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Apr 6, 2004
    #6
  7. Matt

    Bryce (Work) Guest

    On 5 Apr 2004 18:23:17 -0700, (Matt) wrote:

    >In the following code, I need the best practice is (1), but I still
    >don't understand why. So the rule of thumb is if possible, it is best
    >to create an instance of a class that pointed to by interface of that
    >class?
    >
    >HashMap m = new HashMap(); --(1)
    >Map m = new HashMap(); -- (2)
    >
    >Please advise. Thanks!


    IMHO, its best to declare your variable as the most abstract type you
    can use. For example, #2 above would be best, because you can change
    the implementation of m. This reduces the coupling of m on its
    implementation. All your program knows is that its of type Map.

    --
    now with more cowbell
     
    Bryce (Work), Apr 6, 2004
    #7
  8. "Bryce (Work)" <> scribbled the following:
    > On 5 Apr 2004 18:23:17 -0700, (Matt) wrote:
    >>In the following code, I need the best practice is (1), but I still
    >>don't understand why. So the rule of thumb is if possible, it is best
    >>to create an instance of a class that pointed to by interface of that
    >>class?
    >>
    >>HashMap m = new HashMap(); --(1)
    >>Map m = new HashMap(); -- (2)
    >>
    >>Please advise. Thanks!


    > IMHO, its best to declare your variable as the most abstract type you
    > can use. For example, #2 above would be best, because you can change
    > the implementation of m. This reduces the coupling of m on its
    > implementation. All your program knows is that its of type Map.


    Another way to state it would be "declare your variable as the minimum
    set of features you need", just in case you ever get an object that is
    *only* guaranteed to have those features, not any extra features your
    current object might have.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You can pick your friends, you can pick your nose, but you can't pick your
    relatives."
    - MAD Magazine
     
    Joona I Palaste, Apr 6, 2004
    #8
  9. Matt

    Chris Smith Guest

    Tony Morris wrote:
    > HashMap has no additional methods than what Map provides.


    Unfortunately, that's not true. HashMap exposes a public clone() which
    is not specified in the Map interface.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 6, 2004
    #9
  10. Matt

    Tony Morris Guest


    > I think he was asking a more general question, and in general the
    > specific class does have some extra methods the interface does not.


    No it doesn't, take a closer look.
    The only additional methods that are implemented are those of
    java.lang.Object (implying that any interface implementation also has those
    methods).

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Apr 6, 2004
    #10
  11. Matt

    Chris Smith Guest

    Tony Morris wrote:
    >
    > > I think he was asking a more general question, and in general the
    > > specific class does have some extra methods the interface does not.

    >
    > No it doesn't, take a closer look.
    > The only additional methods that are implemented are those of
    > java.lang.Object (implying that any interface implementation also has those
    > methods).


    Again, public clone is quite a different API than the protected clone
    exposed by Object. That constitutes an additional API, and one that is
    often a justification for requiring a specific implementation of a
    Collections interface. (Granted, this is a deficiency in the interface
    rather than a natural implementation; although there's no good reason to
    require knowledge about implementation class in order to clone a Map,
    that knowledge is no less required.)

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 7, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benny Ng
    Replies:
    1
    Views:
    648
    Benny Ng
    Mar 30, 2006
  2. Replies:
    5
    Views:
    4,174
  3. Sridhar R
    Replies:
    14
    Views:
    1,407
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  4. Sandra-24
    Replies:
    18
    Views:
    1,868
    Steve Holden
    Apr 29, 2006
  5. Martin P. Hellwig
    Replies:
    1
    Views:
    377
    Martin P. Hellwig
    Mar 26, 2010
Loading...

Share This Page