Solved

Overriding Interface Methods with Generic Signature

Posted on 2007-03-31
1
1,071 Views
Last Modified: 2013-12-22
I've got an interface ListState<E> which abstacts a couple of methods.  Some of the methods use the generic type E as a parameter, which the compiler is reading as java.lang.Object... so it doesn't think I've properly overridden the methods.

Here's the interface code:
public interface ListState<E>
{
      /**
       * Return list's empty state.
       *
       * @return true if this list's state is empty.
       */
      boolean isEmpty ();
      
      /**
       * Get the size of this list's state.
       *
       * @return the number of elements in this list.
       */
      int size ();
      
      /**
       * Get the element in this list at the given index.
       *
       * @param  index  index position within this list, should be 0 <= index < size()
       * @return the E at position index.
       * @throws IndexOutOfBoundsException if index < 0 or index >= size().
       */
      E get (int index) throws IndexOutOfBoundsException;
      
      /**
       * Get the index of the given element in this list.
       *
       * @param  element  E to search for within this list.
       * @return the index position of the first occurrence of element within this list.
       *         Returns -1 if element is not contained within this list.
       */
      int indexOf (E element);
      
      /**
       * Set the element at the given index in this list.
       *
       * @param  index  index position within this list, should be 0 <= index < size()
       * @param  element  E with which to replace the current E.
       * @throws IndexOutOfBoundsException if index < 0 or index >= size().
       */
      void set (int index, E element) throws IndexOutOfBoundsException;
      
      /**
       * Add the element at the given index in this list.
       *
       * @param  index  index position within this list, should be 0 <= index <= size()
       * @param  element  E to add.
       * @return a new ListState equal to this ListState
       *         with the new E added into the list at position index.
       * @throws IndexOutOfBoundsException if index < 0 or index > size().
       */
      ListState<E> add (int index, E element) throws IndexOutOfBoundsException;
      
      /**
       * Remove the element at the given index in this list.
       *
       * @param  index  index position within this list, should be 0 <= index < size()
       * @return a new ListState equal to this ListState
       *         with the E at position index removed the list.
       * @throws IndexOutOfBoundsException if index < 0 or index >= size().
       */
      ListState<E> remove (int index) throws IndexOutOfBoundsException;
      
      /**
       * Return a string representation of this list.
       *
       * @return a string representation of the elements in this list.
       */
      String toString();
}


Here's a class that's supposed to implement it.  For example, the method public:
       public ListState<E> add (int index, E element) throws IndexOutOfBoundsException
should override the interface method fine... but instead the compiler is reading it as overloading the interface method with a different signature (Element is type E, not type java.lang.Object).  Even that is baffling considering ALL classes are subs of java.lang.Object... right?  Anyways, anyone know how I have to change the syntax to be valid?  FYI I'm using BlueJ.
public class EmptyList<E> implements ListState {
    public boolean isEmpty(){
        return true;
    }
   
    public int size(){
        return 0;
    }
   
    public E get(int index) throws IndexOutOfBoundsException{
        return null;
    }
   
    public int indexOf(E element){
        return -1;
    }
   
    public void set(int index, E element) throws IndexOutOfBoundsException {
        throw new IndexOutOfBoundsException();
    }
   
      public ListState<E> add (int index, E element) throws IndexOutOfBoundsException {
        throw new IndexOutOfBoundsException();
    }
   
    public ListState<E> remove(int index) throws IndexOutOfBoundsException {
        throw new IndexOutOfBoundsException();
    }
   
    public String toString(){
        return "";
    }
}
0
Comment
Question by:netsmithcentral
1 Comment
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 18831107
> public class EmptyList<E> implements ListState {


change that to

public class EmptyList<E> implements ListState<E> {
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 134
servlet  URL Rewriting 1 37
java jdbc batch example 8 35
web application structure 18 77
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question