Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Can't understand why Interfaces are there

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't understand why Interfaces are there

    So I've been searching all over the web what interfaces, and what it comes down to is:
    An interface just says what an object can do, but not how to do it.

    Why would this make me use an interface? When I have to type the method name anyway?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't understand why Interfaces are there

    IMO interfaces are one of the most powerful (and beautiful) features of java, but also can be the hardest to wrap one's head around. It allows a programmer many things from multiple inheritance to abstraction/encapsulate of a behavior. In other words, they provide a great many things that facilitate writing reusable and easily maintainable code.

    If you are familiar with java swing, the *Listener interfaces are a perfect example. All the caller (the swing/awt API) knows is that listeners are registered and are called when appropriate - this allows the swing API to be independent of any solid implementation: anything can be a listener (so long as it implements the appropriate interface). A custom example of this can be found in the following article: How To Use the Observer Pattern

    Lastly, google "java design patterns". They are techniques that are proven to work to write re-usable and easily maintained code, and rely heavily the use of abstraction through interfaces and abstract classes.

    Edit: Cross posted at Can't understand.... While not against the rules, we ask posters be forthright about it. For reasons why, see The Problems with Crossposting
    Last edited by copeg; February 13th, 2011 at 12:24 PM.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand why Interfaces are there

    Quote Originally Posted by copeg View Post
    IMO interfaces are one of the most powerful (and beautiful) features of java, but also can be the hardest to wrap one's head around. It allows a programmer many things from multiple inheritance to abstraction/encapsulate of a behavior. In other words, they provide a great many things that facilitate writing reusable and easily maintainable code.

    If you are familiar with java swing, the *Listener interfaces are a perfect example. All the caller (the swing/awt API) knows is that listeners are registered and are called when appropriate - this allows the swing API to be independent of any solid implementation: anything can be a listener (so long as it implements the appropriate interface). A custom example of this can be found in the following article: How To Use the Observer Pattern

    Lastly, google "java design patterns". They are techniques that are proven to work to write re-usable and easily maintained code, and rely heavily the use of abstraction through interfaces and abstract classes.

    Edit: Cross posted at Can't understand.... While not against the rules, we ask posters be forthright about it. For reasons why, see The Problems with Crossposting

    This is the best comment I've had so far! Thank you good sir!
    I'll invest a few more hours in learning and it'll be ok

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Can't understand why Interfaces are there

    Try designing a plugin acceptable program without them.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand why Interfaces are there

    One more question, are there certain things you can not do unless you implement an interface?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't understand why Interfaces are there

    Quote Originally Posted by vortexnl View Post
    One more question, are there certain things you can not do unless you implement an interface?
    Any behavior defined by a class/implementation that requires an interface cannot be done without the interface implementation. Using my example above, you cannot add an action listener to listen for Action events on, say, a JButton. without implementing the interface.

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand why Interfaces are there

    Quote Originally Posted by copeg View Post
    Any behavior defined by a class/implementation that requires an interface cannot be done without the interface implementation. Using my example above, you cannot add an action listener to listen for Action events on, say, a JButton. without implementing the interface.
    But why did they make that decision that you have to implement an interface to use the action listener? It just doesn't make sense to me.

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't understand why Interfaces are there

    What doesn't make sense? If it were based upon classes: one can only use instances of that class. Based upon interfaces: ANY object can be a listener. Like I said its not an easy concept to wrap one's head around, and usually understanding comes more with experience than anything else.

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand why Interfaces are there

    Quote Originally Posted by copeg View Post
    What doesn't make sense? If it were based upon classes: one can only use instances of that class. Based upon interfaces: ANY object can be a listener. Like I said its not an easy concept to wrap one's head around, and usually understanding comes more with experience than anything else.

    Well what I'm thinking about is for example: actionListeners, let's say I add an actionlistener to a button,
    I could just type
    public void actionPerformed(ActionEvent e)
    right? Why would I need an interface to tell me what's possible when I already know it by default?

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't understand why Interfaces are there

    I could just type...right?
    No...that function defines the interface, but you can't add the method, you must add a class that implements ActionListener - you must tell the compiler there is an ActionListener throughimplements or new ActionListener (for an anonymous class). From the perspective of the class receiving the ActionListener object, all it cares about is the actionPerformed method.

Similar Threads

  1. 2 errors I can't understand
    By Brock in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 27th, 2010, 12:56 AM
  2. JDBC Script-Projet + Swing Interfaces
    By kawazaki123 in forum JDBC & Databases
    Replies: 0
    Last Post: July 5th, 2010, 01:25 PM
  3. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM
  4. Importance of interfaces
    By jyothishey in forum Object Oriented Programming
    Replies: 9
    Last Post: March 12th, 2010, 07:11 AM
  5. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 08:44 PM