简体   繁体   中英

Overriding a Method with Generics Errors

im trying to override the add() but it will not compile

public class AVLTree<E extends Comparable<E>> extends BinaryTree {  
BinaryNode<E> theTree;
    @Override
    public void add (E toInsert)
    {
        if (theTree == null)
    add(toInsert, theTree);     
    }


public class BinaryTree<E extends Comparable<E>> implements Iterable<E> {   
    public void add(E toInsert) { .... }

Error message

java:36: error: name clash: add(E#1) in AVLTree and add(E#2) in BinaryTree have the same erasure, yet neither overrides the other public void add (E toInsert) ^ where E#1,E#2 are type-variables: E#1 extends Comparable declared in class AVLTree E#2 extends Comparable declared in class BinaryTree

As per my comment, BinaryTree is missing a type parameter when specialized by AVLTree .

class BinaryTree<E extends Comparable<E>> implements Iterable<E> {   
    public void add(E toInsert) {
        // ...
    }

    public Iterator<E> iterator() {
        return null;
    }
}

class AVLTree<F extends Comparable<F>> extends BinaryTree<F> {  
    BinaryNode<F> theTree;
    @Override
    public void add (F toInsert)
    {
        // ...     
    }
}

http://ideone.com/dGqd8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM