parameter types, favor interfaces over classes

For parameter types, favor interfaces over classes

I was reading above staemtent. I have not understood the statement, concept behind clearly.
Please advise. Any ideas, sugestions, sample code, links, resources highly appreciated. thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
objectsCommented:

This example shows what it is talking about

Consider this method:

public String getName(HashMap map) {
   return map.get("name");
}

It only uses the get() method of the map parameter. The get() method is defined in the Map interface which HashMap implements so the method is better written as


public String getName(Map map) {
   return map.get("name");
}

We can now pass any class that implements the Map interface  making it more flexible
0
 
for_yanCommented:

Look, all these phrases are fro the book by Joshua Bloch - so read there in the book:

http://books.google.com/books?id=ka2VUBqHiWkC&pg=PA190&lpg=PA190&dq=For+parameter+types,+favor+interfaces+over+classes+java&source=bl&ots=yYCiQhu-Q3&sig=VeRg1QIPLJHa5FUkvoL2fixjMKo&hl=en&ei=QPq2TZyEEKnh0QHdztXKCA&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBkQ6AEwAA#v=onepage&q&f=false

Thsi one is on page 190.

Do you really believe that we here can explain to you
these statements better than the author of this book who supports all of them with good explanation
and examples?

If you don't understand some particular example - ask specifically.
The author would in most cases explain it better - that's why he wrote a book about it.
0
 
Murali MurugesanFull stack Java developerCommented:
Class - some thing like hard coding, where u can use only that particular class type as parameter

Interface - something dynamic, can pass in anything of that type.

Objects, stated clearly with his Map example.

Always prefer Interface than Classes.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.