Results 1 to 4 of 4
  1. #1
    Vortexnl is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Can't understand the purpose of interfaces

    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
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Because interface can offer other behaver in depend from class realization, You can see on interface List and class ArrayList and other, for example.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    To expand, interfaces define behaviour without specifying the details. If you define an interface, you say, any class that implements this interface can do this, this and this. It's also a better alternative to weak superclassing. A weak superclass is one that breakes it's subclasses functionality if altered. Since an interface is just method signatures and constants, this problem is avioded. Also, you can make your code more generic, and thus more open to change. Lets say you're doing something with arraylists, and make a helper method to do something:
    Java Code:
    public void doSomething(ArrayList<Integer> list) {
      //bla bla bla
    }
    Down the line, you realize that using a LinkedList would better suit your needs, so now you need to go back to your code and change ALL of the ArrayList types to LinkedList. If you used interfacing from the start:
    Java Code:
    public void doSomething(List<Integer> list) {
      //yak yak
    }
    you can use any Collection that adheres to the List interface without any changes to your existing code.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. interface purpose?
    By frejon26 in forum New To Java
    Replies: 5
    Last Post: 06-27-2014, 09:41 AM
  2. purpose of a static methods
    By hoosierfan24 in forum New To Java
    Replies: 20
    Last Post: 02-11-2011, 06:58 PM
  3. Don't understand interfaces for inheritance
    By BoomPony in forum New To Java
    Replies: 7
    Last Post: 11-28-2010, 08:23 PM
  4. Purpose of abstract class
    By scott in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-26-2010, 04:21 AM
  5. Over-riding purpose fails..
    By udayadas in forum New To Java
    Replies: 7
    Last Post: 08-24-2008, 04:14 AM

Posting Permissions

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