Solved

Overriding Interface Methods with Generic Signature

Posted on 2007-03-31
1
1,070 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now