views:

20

answers:

1

http://yfrog.com/5iumlp

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 )

A: 

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.

djna
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 --> Yes I do want them to match signature of superclass method, but internally the method should return the correct specialization of JComponent to match the Class so the Button should return JButton.
Thomas Wilczynski
Can you give an example of the interface, As by adding an inteface to the diagram and showing that the two classes realize the interface will solve the signature problem. However How then do i show on the diagram that the subclasses return a specific Subclass of JComponent
Thomas Wilczynski
This diagram is about interfaces: what the caller of a method is promised. You promise to return a JComponent and so that's what you should say on this diagram. You have some additional information for the implementer of the class that they should create a JButton (or maybe a JPrettyButton or ...) but that information belongs somewhere else - as a note or a private method on the class JButton makeComponent();
djna
Thanks for that clarification, by interface i thought you meant create an actual class with <<interface>> in it. UML not my strong point so your answer has really helped me consolidate the usage of notes. Maybe the diagram I posted wasn't clear but i included both a note about it and a method on the sub-classes with the return types. Answer accepted thanks again.
Thomas Wilczynski

related questions