user1724655
user1724655

Reputation: 21

Confusion with generics, interfaces, and extending

So I'm trying to write a program for a generic linked list and skip list. I also wanted to use an interface and superclass while I'm at it, for future use, but I can't seem to figure out how to make everything work together with all the generic types.

This is the basic idea of what I have right now:

My list ADT and node class that everything will extend off of:

public class Node<K extends Comparable<K>> {
    K key;
    Object data;
    ...
}

public interface List<T> {
    public void insert(T value);
    public void delete(T value);
}

And for an example of a linked list:

public class LList<T extends Comparable<T>> implements List<LLNode<T>>
public class LLNode<K extends Comparable<K>> extends Node<K>

Am I doing this right?

Edit: More specifically, I am running into issues when I try to make very basic instances of this class, such as ...

List<String> linkedList = new LList<String>();
List[] lists = {linkedList};
sets[0].insert("cookie");

This gives me a compiler error: java.lang.String cannot be cast to project.LLNode

Upvotes: 2

Views: 287

Answers (1)

HericDenis
HericDenis

Reputation: 1425

I thought in three options:

First, will work with List<String>:

    public class LList<T extends Comparable<T>> implements List<T>
    {
        @Override
        public void insert(T value)
        ...
    }

this way no more Node will be needed.

OR, your second option: you can keep the way you implemented and use List<LLNode<String>>

OR, finally if you need both use Node and this List<String> that I suppose that's your need (it wasn't clear from de question), and use Node s just internally using the first option, if it's not possible, please be clearer on your needs and I'll try to help you.

Regards.

Upvotes: 1

Related Questions