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.


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

Results 1 to 2 of 2

Thread: Doubt in Encapsulation concept

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Thanked 0 Times in 0 Posts

    Default Doubt in Encapsulation concept

    Hi All,

    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.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default 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

Similar Threads

  1. Encapsulation question regarding ArrayLists
    By LDM91 in forum Collections and Generics
    Replies: 3
    Last Post: October 27th, 2010, 11:51 AM
  2. Doubt regarding LIST
    By puneetsr in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2010, 04:19 PM
  3. [SOLVED] The concept of Server and Client
    By rendy.dev in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2010, 04:13 AM
  4. Which javadoc tag is used to denote a comment for a method parameter?
    By TARUN JORA in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2009, 04:30 PM
  5. Replies: 2
    Last Post: October 7th, 2008, 11:03 PM