Results 1 to 3 of 3
  1. #1
    whit3ang3l is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Question Interfaces, what the advantages ?

    We can feel the advantages of class inheritance from at lease sharing
    and reusing the super class's methods. But in an interface, there are
    only abstract methods, and we have implement all the methods in the
    class that implements this interface, why do we still use interface?
    Why don't we define the methods and write their code directly in the
    class without implementing any interface or something like that? In
    other words, what are the advantages of using interface? I have read
    some books, in which the authors just say it's a protocol and so on. I
    still can not understand the necessity of using interface. Can anybody
    give me a sample to illustrate this? Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Interfaces, what the advantages ?

    Another source: What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    Using interfaces allows a class to have more than one data type.
    Because of java's strict datatyping, having more than one type for a class allows one class to be used as an argument to many different methods that each take a different datatype. A common usage is as listeners for the mouse and keyboard events.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Interfaces, what the advantages ?

    Suppose a program P(I) exists that depends on an interface I. Also suppose that you and I both implement that interface I, i.e. Iimpl1 and Iimpl2. That program P doesn't care less what the implementation is, it runs equally fine with either implementation, i.e both P(Iimpl1) and P(Iimpl2) are valid programs without having to rewrite one single line in program P(I). Try to do that without interfaces.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. advantages of Jsp
    By rakesh_mca in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-29-2014, 03:39 PM
  2. How to use Idref Element and its advantages
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:37 PM
  3. What are dependency injection and its advantages
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:27 PM
  4. Advantages
    By samson in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-02-2007, 04:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •