parameter types, favor interfaces over classes

Posted on 2011-04-26
Medium Priority
Last Modified: 2012-06-27
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
Question by:gudii9
LVL 47

Assisted Solution

for_yan earned 200 total points
ID: 35468859

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


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.
LVL 92

Accepted Solution

objects earned 1600 total points
ID: 35471864

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
LVL 13

Assisted Solution

by:Murali Murugesan
Murali Murugesan earned 200 total points
ID: 35488714
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.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question