parameter types, favor interfaces over classes

Posted on 2011-04-26
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


    Look, all these phrases are fro the book by Joshua Bloch - so read there in the book:,+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.
    LVL 92

    Accepted Solution


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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    maxMirror challenge 10 70
    fetching the full url inside controller 2 31
    computer science syllabus 3 41
    Free Alternative to JIRA 4 22
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now