What is the problem with this Java code dealing with Generics?

Posted by devoured elysium on Stack Overflow See other posts from Stack Overflow or by devoured elysium
Published on 2010-04-17T03:40:49Z Indexed on 2010/04/17 3:43 UTC
Read the original article Hit count: 261

Filed under:
|
interface Addable<E> {
    public E add(E x);
    public E sub(E y);
    public E zero();
}

class SumSet<E extends Addable> implements Set<E> {

    private E element;

    public SumSet(E element) {
        this.element = element;
    }

    public E getSum() {
        return element.add(element.zero());
    }
}

It seems that element.add() doesn't return an E extends Addable but rather an Object. Why is that? Has it anything to do with Java not knowing at run-time what the object types really are, so it just assumes them to be Objects(thus requiring a cast)?

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about generics