简体   繁体   中英

implementing interfaces in Java

I wrote a skeleton class that implements the Queue interface. When i compile, i get an ErrMsg asking to implement the add method from Object class(or does it refer to Object interface? I looked up both in the Java API and none of them even has an add method)

Why do i get this error and does it mean that a non-abstract class which implements a given interface needs to implement all the methods from the implemented class? I would have thought not.

import java.util.* ;
class  ArrayQueue implements Queue
{
    private Object[] elems ;
    private int front, rear, length ;

    public static void main(String[] args) 
    {
        System.out.println("Hello World!");
    }

    public ArrayQueue(int maxLength)
    {
        length = 0 ;
        front = 0 ;
        rear = 0 ;
        elems = new Object[maxLength] ;
    }

    public  boolean offer(Object o) {}

    public  Object poll() {}

    public  Object remove() {}

    public  Object peek() {}

    public  Object element() {}
}

compiler message:

ArrayQueue is not abstract and does not override abstract method add(java.lang.Object) in java.util.Queue

btw, is there an other tag than homework to say that this is done in the context of studying? it's not homework i'm doing but its as close as it gets to what i do: study...

[EDIT]

i have been looking at this API page and it doesn't mention add()...am i looking at the wrong page?

http://download.oracle.com/javase/1.5.0/docs/api/

A class that implements an interface must implement all of the methods in the interface, as well as all methods in any other interfaces that the interface inherits.

An abstract class does not need to implement all of the methods in the interfaces it implements, but any concrete classes that inherit the abstract class do.

The entire point of an interface is that any class implementing the interface can be used transparently as an implementation.
If classes didn't need to implement all of their interfaces' members, that would defeat the purpose.

In your example, what would happen if someone writes

Collection myQueue = new ArrayQueue();
myQueue.add("Hi!");

This code calls a method that you didn't write.


The error is telling you that you didn't implement a method named add which takes an Object as a parameter.
The method comes from the Queue interface, as it says at the end of the message.
The Queue interface in turn inherits this method from the Collection interface.

The error message is about the add() method in the Queue interface itself. Note also that Queue extends Collection and Iterable , so you need to implement the methods of those interfaces as well.

An alternative is for your class to extend AbstractQueue - that would save you a lot of work.

A class that implements an interface has to implement all the methods of the interface, unless it is an abstract class. That's what the error means.

To fix it you need to implement the add() method or make the class abstract.

You are missing this method.

If you look at the interface java.util.Queue , you will see that there is a method, add() which takes a single argument, a java.lang.Object .

In order implement the Queue interface, you therefore need to implement that method.

More generally, any time you implement an interface in a class, you need to then write an implementation of every method in that interface (or mark your class as abstract ).

Note that the compiler isn't asking you to implement a method from java.lang.Object , it is asking you to implement an interface whose signature is add(java.lang.Object) which means a method named add with a single argument of type java.lang.Object .

This error message tells that you haven't implemented the method with the following prototype:

add(java.lang.Object)

(so, mentioned Object class is referred to parameter type) and which is declared in Queue interface.

A class which implements an interface not only has to implement all the methods in the interface - it also has to satisfy the entire behaviour contract of the interface. This is because of the Liskov Substitution Principle which states that any subclass of a class (or implementation of an interface) must be usable in place of the superclass, according to the documented behaviour specified in its interface. The language takes care of the fact that it has to have all the class methods, but if you look at the specification for Queue.add(E) , for example, we have:

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

Specified by: add in interface Collection<E>

Parameters: e - the element to add

Returns: true (as specified by Collection.add(E) )

Throws: IllegalStateException - if the element cannot be added at this time due to capacity restrictions; ClassCastException - if the class of the specified element prevents it from being added to this queue; NullPointerException - if the specified element is null and this queue does not permit null elements; IllegalArgumentException - if some property of this element prevents it from being added to this queue

Your implementation has to do everything specified here. Overriding methods is deceptively simple but is generally quite an awkward thing to get right, so as others have suggested it is usually best to minimise how much you override by subclassing a partial implementation like AbstractQueue if possible.

java.util.Queue does have an add method, so you must implement it if you extend Queue .

The reason the message mentioned Object was because the Queue API specifies that the add method takes an argument of type Object .

Because Queue extends Collection , you need a the public boolean add(Object o) method.

It's not asking for a method from Object, just asking for a method that TAKES an Object as a parameter.

Here's the ones you also need to implement:

 boolean    add(E o)
 boolean    addAll(Collection<? extends E> c)
 void   clear()
 boolean    contains(Object o)
 boolean    containsAll(Collection<?> c)
 boolean    equals(Object o)
 int    hashCode()
 boolean    isEmpty()
 Iterator<E>    iterator()
 boolean    remove(Object o)
 boolean    removeAll(Collection<?> c)
 boolean    retainAll(Collection<?> c)
 int    size()
 Object[]   toArray()
<T> T[]

boolean add(E e) is a method of the Queue interface. You must implement it in order to have a fully functioning Queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM