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 6 of 6

Thread: Implementing Multiple Interfaces with Generics

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing Multiple Interfaces with Generics

    I know that it is possible to force generic parameters to implement certain interfaces like the following:

    public class Stuff <E extends Comparable>
    {
       //...
    }

    However, it is not enough for me for E to be Comparable, I also require it to be Cloneable. Is this possible?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Implementing Multiple Interfaces with Generics

    Try
    public class Stuff <E extends Comparable, Comparable>
    {
       //...
    }

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Implementing Multiple Interfaces with Generics

    Unfortunately, I don't think it's possible directly. What you can do is create your class/interface that sets up which interfaces you want to implement:

    public interface ComparableClone<E> extends Comparable<E>, Cloneable
    {
         // nothing needed
    }
    public class Stuff <E extends ComparableClone<?>>
    {
         // ...
    }

  4. #4
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Multiple Interfaces with Generics

    Quote Originally Posted by copeg View Post
    Try
    public class Stuff <E extends Comparable, Comparable>
    {
       //...
    }
    The compiler seems to accept <E extends Comparable, Cloneable>, but as soon as it reaches

    data = element.clone()

    I get:

    C:\Users\username\Documents\Programming>javac BSTree.java -Xlint:unchecked
    BSTree.java:42: cannot find symbol
    symbol  : method clone()
    location: interface java.lang.Comparable
                            data = element.clone();
                                          ^
    1 error

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Multiple Interfaces with Generics

    Quote Originally Posted by helloworld922 View Post
    Unfortunately, I don't think it's possible directly. What you can do is create your class/interface that sets up which interfaces you want to implement:

    public interface ComparableClone<E> extends Comparable<E>, Cloneable
    {
         // nothing needed
    }
    public class Stuff <E extends ComparableClone<?>>
    {
         // ...
    }
    I get nearly the same error, using this implementation as well.

    C:\Users\username\Documents\Programming>javac BSTree.java
    BSTree.java:42: cannot find symbol
    symbol  : method clone()
    location: interface ComparableCloneable<capture#594 of ?>
                            data = element.clone();
                                          ^
    1 error

    Also, I've never seen the ? operator used anywhere other than with ternary operations, what is it's significance when applied to Java Generics?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Implementing Multiple Interfaces with Generics

    Quote Originally Posted by darkestfright View Post
    The compiler seems to accept <E extends Comparable, Cloneable>, but as soon as it reaches

    data = element.clone()

    I get:
    ...
    1 error
    [/CODE]

    Probably because Cloneable is en empty interface, so if you retrieve a Cloneable interface from something you can't call clone unless you are doing so on the object that implements a public form of the method clone. I just played around with it and the following seemed to work for my short test I tried....

    public class YourClass <T extends Comparable<T> & Cloneable>{
     
    }
    Last edited by copeg; February 10th, 2010 at 07:49 PM.

Similar Threads

  1. Importance of interfaces
    By jyothishey in forum Object Oriented Programming
    Replies: 9
    Last Post: March 12th, 2010, 06:11 AM
  2. Identify and avoid some of the pitfalls in learning to use generics
    By JackyRock in forum Java Theory & Questions
    Replies: 0
    Last Post: February 6th, 2010, 04:12 AM
  3. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM
  4. [SOLVED] Implementing push button
    By IDK12 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 10th, 2009, 10:13 AM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM