How to correctly model a changed return type when subclass overrides a super classes method
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.
If your intent is that the child class signatures should return JComponent then surely the diagram should say this, rather than saying that the they return JButton or whatever?
Your diagram should specify the actual interface. At present it's revealing a detail of implementation, that the a JButton object is created.
精彩评论