Results 1 to 2 of 2

Thread: Interfaces

  1. #1
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default Interfaces

    Hi, I'm having a lot of trouble understanding interfaces in java. In my uni assignment, I essentially have to create a registration thing for a bunch of vehicles and I have to make 3 interface classes (each symbolizing 3 different types of vehicles... motor bikes, commercial vehicles, and private vehicles) and all of those are derived classes of the base class "vehicle".

    Then I need a registration class which implements all 3 of those classes.

    The thing which confuses me though is all this abstraction with interface classes.

    Am I correct in saying that every single method you have within an interface class, can't have a body? What is the point of that? Wouldn't that mean I would have to implement all three original interface classes and write out the method bodies within the registration class anyways? Wouldn't it just be easier not to have the 3 other classes.

    I guess what I am asking is, can someone just tell me the point of interface classes.. I'm reading all this material on them, and it just seems as though it tells me how to do them, but I just don't see the point...

    THANKS!

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    I have seen interfaces used 2 ways.

    The first is the way it was designed for: Providing a Frame work for other classes to implement, which allows you to handle just the interface and not the various objects. A good example would be the java.util.List interface(List (Java 2 Platform SE 5.0)). You can choose just to code for a List object.
    Java Code:
         List list = getList();
         list.add(object1);
         list.add(object2);
    You can have getList() return any of the classes that implement the List interface and expect it to provide the same functionality as the others.


    The second way of using Interfaces would be a resting place for global constants. You can list all the constants inside an interface class and have the classes that need to use them just implement the interface.
    Java Code:
    public interface IConstants {
         public static final String LIBRARY = "library";
         public static final int TYPE_INT = 0;
         public static final int TYPE_DOUBLE = 1;

Similar Threads

  1. Interfaces (not GUI)
    By guilty in forum Advanced Java
    Replies: 1
    Last Post: 11-05-2010, 07:16 PM
  2. Interfaces
    By justin1980 in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 12:04 PM
  3. Interfaces
    By computerquip in forum New To Java
    Replies: 19
    Last Post: 09-08-2009, 04:58 PM
  4. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 09:57 PM
  5. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 08:11 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
  •