Doubt in Encapsulation concept
In encapsulation concept can anyone explain this line with example. we can change the datatype of field but no need of changin user class.
•The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.
•A class can have total control over what is stored in its fields.
Re: Doubt in Encapsulation concept
An example for both is the ArrayList class. It functions like an array (and is actually implemented using one), but provides precise control over where in the array you can access.
ArrayList<String> myList = new ArrayList<String>();
myList.add("hello"); // somehow the string "hello" gets added to the ArrayList
System.out.println(myList.get(0)); // somehow the zeroth element inside myList gets extracted. No need to know where in myList this is actually stored
System.out.println(myList.get(1)); //The get method has complete control over where you can retrieve data from. The get method generates an exception because there is one string in myList even though the actual implementation could have a slot for a second string