Hi Zero
thanks a lot for the help, I used your advice in the form
public interface myComparator extends Comparator
{
// ...
}
class TestClass
{
public static void main(String args[])
{
TreeMap myMap = new TreeMap(myComparator);
}
}
Um no that is what you had, not what I suggested. If it's working I
guess you just mis-quoted
and it is working. However, I feel somewhat confused:
- The constructor for TreeMap prescribes explicitly
TreeMap(Comparator c). myComparator is not formally a Comparator, it
is an instance of a class. This class could do a lot of other things
and perhaps have more implemented interfaces. (Is it so?)
yep that's correct.
Java
compiler is usually very strict about the exact form of arguments, so
I am surprised that it takes something for a comparator, if "served on
a plate of a class".
Well the thing to understand here is that an instance of a subclass *is
an* instance of a superclass. A classical example:
class Car { ... }
class Mercedes extends Car { ... }
Here Mercedes *is a* Car. This means that everywhere where the compiler
expects a Car object, you can use a Mercedes instead, and it won't
complain.
The same goes for interfaces. MyComparator *is a* Comparator.
- Interface cannot be instantiated. But there seems to exists some
hidden form of instance, since the method myMap.comparator() returns a
concrete comparator, which sounds to me very much like an instance.
There are actually two ways to do this: create an object of a class that
implements the interface, and send that back (see above, it is an
instance of the interface, as well as of the class), or create an
anonymous class that implements the interface. This is in fact the same
as creating a normal class, it just looks different. And this is
probably the way it's done in TreeMap:comparator().
For example:
public Comparator comparator()
{
return new Comparator()
{
public int compareTo(Object o)
{
return 0;
}
};
}
This looks like a Comparator is being instantiated - but in fact this
creates a class with no name that implements the Comparator interface.
Try looking for "anonymous class" online if you want a more complete
explanation.
- I appreciate very much the ability of Java compiler to recognize
a comparator (which is per definition an interface) in the instance of
a class, but don't know, how much I can generalize this for other
applications. Does a class with more implemented interfaces live
multiple life and shows "multiple faces" to the compiler? Again,
this is a very admirable ability.
Pretty much yes. If you have a class like this:
class MyClass extends BaseClass implements Interface1, Interface2
{
// ...
}
then objects of this class are instances of MyClass, BaseClass,
Interface1, Interface2, plus any classes that BaseClass inherits from,
and any interfaces any of those base classes implement. So yes an object
of type MyClass has a lot of faces to show the compiler, as you said
Btw all objects are also instances of class Object, even if you don't
explicitly extend Object.
I am afraid, that I don't think sufficiently "Java" yet. (I
program since 15 years in G2 and am a greenhorn in Java). I would
appreciate your comment. Thanks again.
regards
ludwig
Practice
And get a good book about Object Orientation. Your
questions are not really specific to Java (though the details are), they
are fundamental OO concepts. One possibility is UML 2 Toolkit by
Eriksson, but there are many others.