Pratik Mehta
Pratik Mehta

Reputation: 345

Enumeration methods

Enumeration interface has method hashMoreElements is used with refrence variable (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?

Upvotes: 0

Views: 1057

Answers (5)

Colin Hebert
Colin Hebert

Reputation: 93177

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 :

Upvotes: 1

Buhake Sindi
Buhake Sindi

Reputation: 89189

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).

Upvotes: 3

Michael Borgwardt
Michael Borgwardt

Reputation: 346387

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.

Upvotes: 0

Darron
Darron

Reputation: 21610

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502216

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.)

Upvotes: 3

Related Questions