Solved

Overriding Interface Methods with Generic Signature

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

17 Experts available now in Live!

Get 1:1 Help Now