Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Overriding Interface Methods with Generic Signature

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

808 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