Explore Java: UnsupportedOperationException :(

Please read this

In the creation of the Collections Framework, the Sun development team needed to provide flexible interfaces that manipulated groups of elements. To keep the design simple, instead of providing separate interfaces for optional capabilities, the interfaces define all the methods an implementation class may provide. However,some of the interface methods are optional. Because an interface implementation must provide implementations for all the interface methods, there needed to be a way for a caller to know if an optional method is not supported. The manner the framework development team chose to signal callers when an optional method is called was to thrown an UnsupportedOperationException. If in the course of using a collection, an UnsupportedOperationException is thrown, like trying to add to a read-only collection, then the operation failed because it is not supported.

Now my question : how can an interface have optional implementation for some methods and how does java do it ?

[1045 byte] By [Java@Harmeet] at [2007-9-30 18:48:29]
# 1

> Now my question : how can an interface have

> optional implementation for some methods and how does

> java do it ?

The methods are still implemented in the sense you'd expect the language requires them to be. However, the go against the spirit of implementing an interface by just throwing UnsupportedOperationException. Something like this: public class MyUnmodifiableList implements List {

public void add() { // here's the method, so the compiler's happy

throw new UnsupportedOperationException("Did the name 'Unmodifiable' not give you a clue, genius?");

}

... other methods ...

}

So it's not that the methods aren't implemented in the literal Java language sense. Rather, they're "not implemented" in the sense that they don't do what the docs say they're supposed to--don't do anything by throw an exception.

Furthermore, there's nothing in the language that lets you say "this is an optional method." The concept of a method being "optional" exists only in the documentation for that interface.

jverd at 2007-7-6 21:05:40 > top of Java-index,Security,Event Handling...
# 2

Hi,

it is described in the text you provided:

>Because an interface implementation must provide implementations for all the interface methods,

> there needed to be a way for a caller to know if an optional method is not supported.

Means, in Java you must implement all interface methods, but the implementation can be empty. But if you leave an implementation empty you should notify the programmer about it during runtime.

> The manner the framework development team chose to signal callers when an optional method is

> called was to thrown an UnsupportedOperationException.

The programmers of the collections framework throw a UnsupportedOperationException if you call an optional interface method that is not really implemented.

Of cource all the interface methods are implemented, but some of them will only throw an UnsupportedOperationException.

Andre

twupack at 2007-7-6 21:05:40 > top of Java-index,Security,Event Handling...
# 3
Thanx for such a prompt reply jverd and to you also twupack
Java@Harmeet at 2007-7-6 21:05:40 > top of Java-index,Security,Event Handling...