Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Encapsulation question regarding ArrayLists

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Encapsulation question regarding ArrayLists

    Hi there. I've been working on a side project outside of class (I'm a Software Engineering student at Uni) which is really just a simple Banking system to help my understanding of Java. Basically the program reads existing accounts from a .txt file which are then placed into an ArrayList (they are split into Account objects).

    The user can then choose to create a new account which added to the ArrayList or edit an existing one. I have three classes - the main class, Account class and UserInterface class. The main class holds all the methods such as SaveAccount (writes the accounts into file) etc. the Account class defines the account and then the UserInterface produces a UI to the user and interacts with them. The problem is I have the ArrayList in the main class and the only way to access it in UserInface is through making it public. My lecturer said this is bad since then the UserInterface class can edit it which I don't want. The thing is though I can't make the Account methods private as I want to edit them until they are put into the ArrayList so I'm not sure how to do it.

    My only theory is copying the object to a duplicate class which has private settings before throwing it into the ArrayList. Would this work/be the best solution? Sorry if my question is long winded, I'll try to make it more concise if necessary.

    P.S I wasn't sure if this should go in theory as I'm not posting code but it sort of addresses ArrayLists directly


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default Re: Encapsulation question regarding ArrayLists

    If I understand your question correctly, you don't want to have a method with returns the full arraylist for fear of that class modifying the list, but without it how can another class access its contents?
    public List getList(){
    ...
    }
    If so, create a method which retrieves an object in the List at a particular position
    public Object getItem(int pos){
    ...
    }

    Or somehow index those objects so there is an easy way for the client to access an object by key or position

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Encapsulation question regarding ArrayLists

    I think you have understood what I was trying to say, but retrieving the objects that way would still require the ArrayList to be public to be read from would it not?

    Basically I need public objects until their are read into an uneditable ArrayList which can still be seen in another class. Are you trying to suggest that I set all details of object (aka account) and then pass it into a list? If so does a list just act like an ArrayList or Array? (aka you can select an index to specify which account) My account objects have an ID, Type and OverdraftLimit.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default Re: Encapsulation question regarding ArrayLists

    By providing a public method to access a value in the array rather than the array itself, the client has no clue how the data is stored and thus the ArrayList is encapsulated: the client doesn't care how the objects are stored so long as the method retrieves an object specified by the parameter (there are several ways to deal with whether the said object doesn't exist).

    The client (UserInterface) then can add a new account to the main class, or if it wishes to edit an account it does so through the accessor method I outlined above.

Similar Threads

  1. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  2. [SOLVED] How to dynamically create ArrayLists (or something similar)?
    By igniteflow in forum Collections and Generics
    Replies: 4
    Last Post: August 6th, 2009, 06:00 AM