Here we have a class diagram for a personal program I am working on. The question I am asking is whether I have modelled the subclasses of UserControl Correctly. Specifically the base class has a method public abstract JComponent toComponent()
. During implementation the subclasses should override the method keeping the method signature the same except that the return statement should return a specialized Component ie return new JButton();
While I the creator of the diagram understand how the implementation should look I can see how another programmer may look at the diagram and come to the following conclusion.
public JButton toComponent(){
if(GUIComponent == null || !(GUIComponent instanceof JButton)){
return new JButton();
} else{
return (JButton) GUIComponent;
}
}
Where the intended implementation is:
public JComponent toComponent(){
if(GUIComponent == null || !(GUIComponent instanceof JButton)){
return new JButton();
} else{
return GUIComponent;
}
}
In this instance the implementation may not be of great concern, however in a more complex system it may prove to be crucial. So I would like to know how to correctly model the overriding of a method to return a different type while keeping the signature exact. Thanks for your input my first post here. (sorry for image link it would not let me post until i get 10 rep points )