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
  • 1
    Just a note: The answer was self deleted.
    – yannis Mod
    Commented Aug 19, 2012 at 16:21

2 Answers 2

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.

2
  • if this explanation was written above the code you wrote in original answer, I would probably be the first to upvote it (btw since the answer is self-deleted you can edit and restore it)
    – gnat
    Commented Aug 21, 2012 at 8:47
  • 1
    @gnat I edited and reopen the answer programmers.stackexchange.com/questions/154794/… Commented Aug 7, 2014 at 2:50

You must log in to answer this question.

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