I’m using Apache Commons for the first time, mainly because I wanted access to their HashBag class. I’ll outline the entire process I’ve followed to help pinpoint the problem.
First, I downloaded current version (3.2.1) of the Apache Commons Collections.
http://commons.apache.org/collections/download_collections.cgi
Then I extracted the file commons-collections-3.2.1.jar to a folder on my disk, and added that folder to my system %CLASSPATH% environment variable so that I could import classes into my Java programs.
This test program illustrates the problem I’m having.
import org.apache.commons.collections.bag.HashBag;
public class test {
public static void main(String[] args) {
HashBag test = new HashBag();
System.out.println(test.getClass().getName());
System.out.println(test.uniqueSet().getClass().getName());
}
}
The first output line says org.apache.commons.collections.bag.HashBag which is expected.
The second output line says org.apache.commons.collections.set.UnmodifiableSet which is NOT expected. According to the doc HERE, the uniqueSet() method is inherited from both a superclass and an interface, but in both of those cases the method is supposed to return a java.util.Set. Why is it returning something different here?
Thanks!
There is no problem at all,
org.apache.commons.collections.set.UnmodifiableSetimplementsjava.util.Set.A function with a given return statement can return descendants of the specified class. Otherwise, it would not be possible to use interfaces and abstract classes as return types…
That’s OOP at its finest 🙂