Results 1 to 4 of 4
  1. #1
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default Can generic types implement an interface?

    Is there any way to do make a generic type declaration such that the type implements some interface?

    For example, suppose I want to write a method that sorts anything that implements comparable (further, suppose for academic reasons that I do not want to use Collections.Sort). Then I want a generic type T such that T implements Comparable. Yet I believe the following code is invalid java:

    Java Code:
    public static <T implements Comparable> T sort(Collection<T> coll){
    		...sorting algorithm goes here...
    	}
    How can I work around this?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    I'm no expert in Generics, but I believe that should be
    Java Code:
    <T extends Comparable>
    or
    Java Code:
    <T extends Comparable<T>>
    db

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    I didn't know the answer either, but a quick Google search (Francisoud's Blog: Java generics and interface) supports what Darryl.Burke says.

  4. #4
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default

    Burke is right I need to extend Comparable. What I want is a method that works over all types T such that T is comparable, to return a collection of type T. The correct type signature of the method is
    Java Code:
    	public static <T extends Comparable<T>> Collection<T> sort(Collection<T> coll){
    		return coll;		
    	}
    Then the next step is not to figure out how to sort it, that is just a sorting algorithm. The question is, does this method actually make sense. Does it make sense to sort any collection?

    No, of course not. Lists should be sorted, and arrays, and arrayLists, and maybe some others, vector? But Tree, Set, Map, Bag? I can't find any reason to sort these. So, instead of a method that sorts a collection of Type<T> and returns a collection of type<T>, perhaps it is more appropriate to identify some collection interface, where every implementor is sortable, and go from there. I don't really know my way around collections. Any suggestions?

Similar Threads

  1. Replies: 1
    Last Post: 10-20-2008, 01:58 PM
  2. How to implement the Icon interface in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:17 AM
  3. A generic interface example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:42 PM
  4. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:12 AM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 11:43 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
  •