Java puzzle 28: Improving encapsulation

<< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
 P25   P26   P27   P28   P29   P30 
 By title 
>>

If the following code is compiled and run, it writes [Orange, Apple, Mango, Cherry] to the standard output. By adding cherry to the fruit’s list, it’s clear that the class TestFruit is able to modify the fruit’s list. How can we improve the encapsulation of the class Fruit to prevent other classes from removing and adding elements to it? To achieve the previous goal, replace the statement return fruits; at line 27 with only one other statement.
As a result of that this code writes the original elements, namely [Orange, Apple, Mango] to the standard output.
What is that statement?

Java puzzle 28: Improving encapsulation

Level: Hard

import java.util.ArrayList;

public class TestFruit
{
  public static void main(String[] args)
  {
    Fruit fruit = new Fruit();
    fruit.getFruits().add("Cherry");
    System.out.print(fruit.getFruits());
  }
}
class Fruit
{
  private ArrayList<String> fruits;
  {
    fruits = new ArrayList<>();
    fruits.add("Orange");
    fruits.add("Apple");
    fruits.add("Mango");
  }
  
  public ArrayList<String> getFruits()
  {
    /*  replace the following statement
     *  with only one statement
     */
    return fruits;
  }
}
Author: Sar Maroof

Answer explanation

Adding and removing elements  from outside the class can be prevented by returning a copy of the list as follows.
Replace the statement at line 27: return fruits; with the following statement

return new ArrayList(fruits);
You can also return a clone of the ArrayList as follows.
return (ArrayList<String>) fruits.clone();

In every situation a programmer needs to make the choice that fits his/her needs, there might be also other solutions. I don’t get to the details of the choices, because that depends on the cases. What is presented here could be a simple solution! After all this is a puzzle that shows some idea’s and tricks to learn.

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017