简体   繁体   中英

Java Generic Interface method-overload

I'm trying to make a interface for my Task sheet at university. I have to implement all this methods for various data-structs so I would like to implement this interface.

The Problem is, the data-structs have to be generic ex: LinearList<T> where the type of key is T. Now different data-structs have different Elements ex. a LinearList<T> has ListItem<T> as elements and Trees have TreeNode<T> .

So I thought I make a interface with <C, T> where C = ListItem and T the type ex. Integer.

Now i have some overloaded methods ex:

insert(T key)
insert(ListItem<T> item)

so the user can add a key or he can add for example the head of another list. But now I'm getting a compiler error i dont understand:

java:15: error: name clash: insert(T,int) and insert(C,int) have the same erasure
    boolean insert(T key, int pos);

What can I do to enable overloading the way I explained above? Because in a abstract class I tried it and it worked.

Edit: Okay like in the comments discussed I use different method names now. It seams to be the solution other collections use to solve the problem and as mentioned hides(?) more implementation details. Many thanks for the support!

package interfaces;

/**
 * Interface with all methods for the data-structs I have to learn for the exam
 *
 * <C> is the class of the Elements to be entered ex: ListItem<T>
 * <T> the type of the elements stored in the data-structs
 * 
 * @author 
 */
public interface ExamPreparation<C, T> {

    boolean insert(C item, int pos);

    boolean insert(T key, int pos);

    boolean insertAtHead(C item);

    boolean insertAtHead(T key);

    boolean insertAtTail(C item);

    boolean insertAtTail(T key);

    boolean insertSorted(C item, Comparator<T> comp);

    boolean insertSorted(T key, Comparator<T> comp);


    // ========== Remove Methods ==========

    boolean remove(T key);

    boolean removeAll(T key);


    // ========== Overwrite Methods ==========

    /**
     * takes the first appearance of oldKey and overwrites it wit
    h newKey
     *
     * @param newKey
     * @param oldKey
     * @return true if overwrited. False if oldKey is not in list
     */
    boolean overwrite(T newKey, T oldKey);

    /**
     * takes all the oldKeys and overwrites it with the newKey
     *
     * @param newKey
     * @param oldKey
     * @return returns true if at least one oldkey was found
     */
    boolean overwriteAll(T newKey, T oldKey);

    /**
     * overwrite at position
     *
     * @param newKey
     * @param pos
     * @return returns false if pos is not valid else true
     */
    boolean overwriteAt(T newKey, int pos);


    // ========== Other ==========

    boolean contains(T key); 
}

How about declaring a new interface

interface ElementType<T> {

}

and then making the possible element types; extend or implement ElememtType like this:

interface ListItem<T> extends ElementType<T> {

}

interface TreeNode<T> extends ElementType<T> {

}

and finally changing the declaration of ExamPreparation from

public interface ExamPreparation<C,  T>

to

public interface ExamPreparation<C extends ElementType<T>,  T>

So that the following code compiles with no problem;

    ListItem<String> item = null;
    ExamPreparation<ListItem<String>, String> examPreparation = null;
    boolean isInserted;
    isInserted = examPreparation.insert("12", 0);
    isInserted = examPreparation.insert(item, 0);

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