开发者

Enumeration methods

Enumeration interface has method hashMoreElements is used with refrence v开发者_运维百科ariable (Enumv), how can they be used since they are not implemented ? I meant it is an interface method so how can it be called - Enumv.hasMoreElements() it does not have a implementation .

Vector v = new Vector();
//v contains list of elements - Suppose
Enumeration Enumv = v.elements();
while(Enumv.hasMoreElements()) {
    System.out.println(Enumv.nextElement());
}

How is this possible?


For your code,

Enumeration Enumv = v.elements();

This is what Vector is returning to you.

public Enumeration<E> elements() {
    return new Enumeration<E>() {
        int count = 0;

        public boolean hasMoreElements() {
        return count < elementCount;
        }

        public E nextElement() {
        synchronized (Vector.this) {
            if (count < elementCount) {
            return (E)elementData[count++];
            }
        }
        throw new NoSuchElementException("Vector Enumeration");
        }
    };
}

As you can see, Vector returns an implemented Enumeration class (which annonymous to the developer).

so how can it be called - Enumv.hasMoreElements() it does not have a implementation .

It is implemented (as shown above).


Enumeration is an interface. It provides the method definitions, but anything implementing the interface has to provide the implementation. The point is that code using the object via the interface doesn't need to know about the implementation - just that something is implementing the relevant methods.

In the case of the value returned by Vector.elements(), the Enumeration implementation is actually provided via an anonymous inner class - but the caller doesn't need to care about that at all.

(Note that this isn't specific to Enumeration - it's the general principle of interfaces.)


Vector.elements() doesn't simply return "an enumeration".
In reallity it returns an instance of an implementation of Enumeration but an implementation of Enumeration "is-a" Enumeration and can be "casted" to Enumeration interface.

So the method is implemented, that's why it works.


In the Vector case the implementation is an anonymous class directly based on Enumeration.

public Enumeration<E> elements() {
    return new Enumeration<E>() {
        int count = 0;

        public boolean hasMoreElements() {
            return count < elementCount;
        }

        public E nextElement() {
            synchronized (Vector.this) {
                if (count < elementCount) {
                    return elementData(count++);
                }
            }
            throw new NoSuchElementException("Vector Enumeration");
        }
    };
}

Resources :

  • grepcode - Vector.elements()


I don't understand your question completely.

The class Vector has an implementation of the elements() method, that returns an Enumeration. The concrete implementation of Enumeration that is returned is required to have an implementation of the hasMoreElements() method.


The elements() method of the Vector class returns an instance of an anonymous class that implements the methods of the Enumeration interface. You don't need to know anything about this class, since the interface defines how it should behave - that's exactly the point of interfaces.

Note that Vector and Enumeration are both obsolete and should not be used anymore. Instead, use ArrayList and Iterator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜