Question about gets and sets and when to use super classes

Posted by Nazgulled on Stack Overflow See other posts from Stack Overflow or by Nazgulled
Published on 2010-05-22T22:54:54Z Indexed on 2010/05/22 23:00 UTC
Read the original article Hit count: 262

Filed under:
|
|
|
|

Hi,

I have the following get method:

public List<PersonalMessage> getMessagesList() {
    List<PersonalMessage> newList = new ArrayList<PersonalMessage>();

    for(PersonalMessage pMessage : this.listMessages) {
        newList.add(pMessage.clone());
    }

    return newList;
}

And you can see that if I need to change the implementation from ArrayList to something else, I can easily do it and I just have to change the initialization of newList and all other code that depends on what getMessageList() returns will still work.

Then I have this set method:

public void setMessagesList(ArrayList<PersonalMessage> listMessages) {
    this.listMessages = listMessages;
}

My question is, should I use List instead of `ArrayList in the method signature?

I have decided to use ArrayList because this way I can force the implementation I want, otherwise there could be a mess with different types of lists here and there.

But I'm not sure if this is the way to go...

© Stack Overflow or respective owner

Related posts about java

Related posts about interface