Solved

Overriding Interface Methods with Generic Signature

Posted on 2007-03-31
1
1,075 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
[X]
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
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

688 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