Results 1 to 6 of 6
  1. #1
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Question Interfaces using generic tipe constrinctions

    I want to make some library interfaces for a graph.
    Using these interfaces:

    Graph<V,E>

    Edge<E>

    Vertex<V>

    how can i constraint users of this library to use the same type <E> in the graph and edge interface and type <V> in the graph and vertex interface??

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Interfaces using generic tipe constrinctions

    The Edge and Vertex classes probably don't need to be generically typed. But your Graph class might be something like

    public class Graph<V extends Vertex, E extends Edge>

    That's the easy part.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Interfaces using generic tipe constrinctions

    Jim might be right about not needing to make the edges and vertices generic. Or maybe you do want to make them generic, if they're supposed to carry references to something they represent. Let's say that you do make them generic. If you also genericize your add() methods, etc., the compiler itself will enforce what you want. That's the whole point of generics.

    Java Code:
    Graph<Foo, Bar> graph = new Graph<Foo, Bar>();
    Vertex vertex = new Vertex<Qux>();
    graph.add(vertex); // compile-time error!
    Get in the habit of using standard Java naming conventions!

  4. #4
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Interfaces using generic tipe constrinctions

    ok thenks guys!
    another question: In my graph class i shuold have a collection of edges and vertices i'm not sure of which type should i use set or list. what do you think?

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Interfaces using generic tipe constrinctions

    What operations are those collections going to support?
    Get in the habit of using standard Java naming conventions!

  6. #6
    dvs_sebba is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Interfaces using generic tipe constrinctions

    add and remove operations that wil be used rarily
    find vertex or edge this will be oftenly used anyway in the vertex and edge class i'll maintain a refereance to it's position in the colection mainly an index for the list or the hash code for the set

Similar Threads

  1. Interfaces
    By marcvaf in forum New To Java
    Replies: 4
    Last Post: 01-15-2014, 05:55 PM

Posting Permissions

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