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

M

Matt

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!
 
T

Tony Morris

Matt said:
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
 
R

Roedy Green

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.
 
T

Tony Morris

Roedy Green said:
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.

--
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
 
R

Roedy Green

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.
 
C

Christophe Vanfleteren

Tony said:
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.
 
B

Bryce (Work)

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.
 
J

Joona I Palaste

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 ([email protected]) ------------- 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
 
C

Chris Smith

Tony said:
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
 
T

Tony Morris

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
 
C

Chris Smith

Tony said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top