0

I am working on modifying a fairly basic linked list implementation of a log in Java to use generics. My project has 3 .java files:

  • GenericLogInterface.java -> Interface that defines the log
  • LLGenericNode.java -> Class that defines the LL's nodes
  • LinkedGenericLog.java -> Extension of interface that defines the LL

Unfortunately, I am receiving a compile-time error from LinkedGenericLog.java:

ch02/genericStringLogs/LinkedGenericLog.java:10: ch02.genericStringLogs.LinkedGenericLog is not abstract and does not override abstract method contains(java.lang.Object) in ch02.genericStringLogs.GenericLogInterface

This would seemingly be easily solved by overriding the contains() method from GenericLogInterface.java in LinkedGenericLog.java.

There is one huge hangup, however: I have already overridden it.

Here is the source for the three .java files:

GenericLogInterface.java

package ch02.genericStringLogs;

public interface GenericLogInterface<T>
{
  void insert(T element);
  // Precondition:   This GenericLog is not full.
  //
  // Places element into this GenericLog.

  boolean isFull();
  // Returns true if this GenericLog is full, otherwise returns false.

  int size();
  // Returns the number of Strings in this GenericLog.

  boolean contains(T element);
  // Returns true if element is in this GenericLog,
  // otherwise returns false.
  // Ignores case differences when doing string comparison.

  void clear();
  // Makes this GenericLog empty.

  String getName();
  // Returns the name of this GenericLog.

  String toString();
  // Returns a nicely formatted string representing this GenericLog.
}

LLGenericNode.java

package ch02.genericStringLogs;

public class LLGenericNode<T>
{
  private T info;
  private LLGenericNode link;

  public LLGenericNode(T info)
  {
    this.info = info;
    link = null;
  }

  public void setInfo(T info)
  // Sets info of this LLGenericNode.
  {
    this.info = info;
  }

  public T getInfo()
  // Returns info of this LLGenericNode.
  {
    return info;
  }

  public void setLink(LLGenericNode link)
  // Sets link of this LLGenericNode.
  {
    this.link = link;
  }

  public LLGenericNode getLink()
  // Returns link of this LLGenericNode.
  {
    return link;
  }
}

LinkedGenericLog.java

package ch02.genericStringLogs;

public class LinkedGenericLog<T> implements GenericLogInterface
{
  protected LLGenericNode log; // reference to first node of linked
                              // list that holds the GenericLog items
  protected String name;      // name of this GenericLog

  public LinkedGenericLog(String name)
  // Instantiates and returns a reference to an empty GenericLog object
  // with name "name".
  {
    log = null;
    this.name = name;
  }

  public void insert(T element)
  // Precondition:   This GenericLog is not full.
  //
  // Places element into this GenericLog.
  {
    LLGenericNode newNode = new LLGenericNode(element);
    newNode.setLink(log);
    log = newNode;
  }

  public boolean isFull()
  // Returns true if this GenericLog is full, false otherwise.
  {
    return false;
  }

  public int size()
  // Returns the number of items in this GenericLog.
  {
    int count = 0;
    LLGenericNode node;
    node = log;
while (node != null)
    {
      count++;
      node = node.getLink();
    }
    return count;
  }

  public boolean contains(T element)
  // Returns true if element is in this GenericLog,
  // otherwise returns false.
  // Ignores case difference when doing comparison.
  {
    LLGenericNode node;
    node = log;

    while (node != null)
    {
      if (element.equals(node.getInfo()))  // if they match
        return true;
      else
        node = node.getLink();
    }

   return false;
  }

  public void clear()
  // Makes this GenericLog empty.
  {
    log = null;
  }

  public String getName()
  // Returns the name of this GenericLog.
  {
    return name;
  }

  public String toString()
  // Returns a nicely formatted string representing this GenericLog.
  {
    String logString = "Log: " + name + "\n\n";
    LLGenericNode node;
node = log;
    int count = 0;

    while (node != null)
    {
      count++;
      logString = logString + count + ". " + node.getInfo() + "\n";
      node = node.getLink();
    }

    return logString;
  }
}

As you can clearly, see, I have overridden contains() in LinkedGenericLog.java Yet, the compiler still throws me this error. I am thinking it has to do with my use of generics in the argument of the contains() method, but I am new to generics and can't understand the problem.

Can anyone help me?

(Btw I am running java version "1.6.0_15" and compiling with the command line)

2
  • 1
    What is the T in LinkedGenericLog? Feb 27, 2013 at 21:20
  • T represents the type of item that the LinkedGenericLog is meant to store. In other words, you could instantiate a LinkedGenericLog where T is replaced by String, and then use the Log to store String objects.
    – bean
    Feb 27, 2013 at 21:22

2 Answers 2

3

In java the method signature includes the parameters and their types. SO I would have to say that the type of T in LinkedGenericLog is different from the one in GenericLogInterface

public class LinkedGenericLog<T> implements GenericLogInterface

This line doesn't indicate that T is the same, you should replace it with:

public class LinkedGenericLog<T> implements GenericLogInterface<T>
2
  • Thanks very much, the error was not displayed. I'll set this question to solved :)
    – bean
    Feb 27, 2013 at 21:25
  • @user2117153: Note that that's not all you should do. You're using raw types all over the place - see my answer.
    – Jon Skeet
    Feb 27, 2013 at 21:26
2

The problem is that you're implementing the raw type of GenericLogInterface. That's causing all kinds of problems, with two different type parameters involved. All you need to to is change this:

public class LinkedGenericLog<T> implements GenericLogInterface<T>

to this:

public class LinkedGenericLog<T> implements GenericLogInterface

Then you should also stop using the raw types for LLGenericNode. So:

protected LLGenericNode log;

should be:

protected LLGenericNode<T> log;

and this (in insert):

LLGenericNode newNode = new LLGenericNode(element);

should be:

LLGenericNode<T> newNode = new LLGenericNode<T>(element);

Ditto uses of LLGenericNode in toString() and contains().

... and also within LLGenericNode itself. Basically, everywhere you're using a raw type, stop doing that :)

1
  • Thanks Jon. I'll be sure to look into these issues and do some research on raw types so I don't make these errors in the future.
    – bean
    Feb 27, 2013 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.