2

The question is: Use constructor or setter method?

...I am not sure if that is a correct way to deal with problem. I think in above mentioned case user is anyway going to change the text, so why not force him while constructing the action?

I am not quite comfortable with this answer. It contains nothing but code and the code does not look self-explanatory to me:

public abstract class Action {
  protected String text = "Default action text";
  protected String toolTip = "Default action tool tip";
  protected String imageURl = "http://myserver.com/images/default.png";

  .... rest of code, I guess setters and getters
}

public class MyAction extends Action {

    public MyAction() {
    }

    public MyAction(String actionText) {
        setText(actionText);
    }

    public MyAction(String actionText, String toolTip_) {
        setText(actionText);
        setToolTip(toolTip_); 
    }

    public MyAction(String actionText, String toolTip_; String imageURL_) {
        setText(actionText);
        setToolTip(toolTip_);
        setImageURL(imageURL_);
    }
}

Am I missing something here? Could above be really a valuable answer to the question asked?

update

As of now, answer is removed (self deleted).

  • 1
    Just a note: The answer was self deleted. – yannis Aug 19 '12 at 16:21
2

The code doesn't really answer the asker's question or add value, IMO.

A similar question has been asked on MSO here. Generally on SO I see a lot of "code only" answers get down-voted and/or deleted.

The original question is more about "why" and "is this correct" rather than a "I need the code to do this".

1

That answer was mine, what I tried to do was the following:

Superclass is abstract and has all three members set to a default value.

Subclass has different constructors so the programmer can instantiate it.

If first constructor is used, all members will have the default values.

If second constructor is used, you give an initial value to actionText member leaving other two members with the default value...

If third constructor is used, you instantiate it with a new value for actionText and toolTip, leaving imageURl whith the default value...

And so on.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .