I have a base-class called Element. Some other classes (like Label and Image) both extend this class.
I now have a dispatching class having the following methods:
public class Dispatcher {
public static AbstractPropertyEditor<Label> createEditor(Label e) {
...
}
public static AbstractPropertyEditor<Element> createEditor(Element e) {
...
}
}
If now I have an instance of Label (which extends Element) and I want to pass it to createEditor(), why is the most generic method (the second one) called? Wouldn’t it be normal that the most specific method (createEditor(Label e)) is called?
I absolutely need the method with the Element-param in order to “catch” all those classes that a) implement Element but do not have their own specific method in this dispatching class..
I’m using Java 6, how to “fix” this?
Edit: Okay, I have to admit it’s not at all about generics. But that’s where I encountered it the first time.
thanks and regards
Why don’t you:
Elementabstract class that provides a defaultcreateEditor()implementationLabeloverride thecreateEditor().Thus you won’t need the static utilities and will achieve your goal.
If you need
Elementto be an interface, then:createEditor()as methods ofElementEditorFactoryinterfaceDefaultEditorFactoryandListEditorFactoryuse the appropriate factories in the implementors of
Element:where the concrete
EditorFactoryis instantiated either during initialization or via some sort of dependecy-injection.As per your concrete question – it depends on what type you have compiled there. If you call
createEditor(obj)it will depend whether it’sElement obj = ..orLabel obj = ..