Results 1 to 9 of 9
  1. #1
    NareshN is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default difference between accessing the variable directly and using properties

    Hi All,

    can u tell me difference between accessing the EmpId directly and using properties.How we can prevent data loss.This concept is called Encapsulation.
    what happens if i use varibles direrctly.please explain me clearly

    int m_employeeId;
    string m_employeeName;
    public int EmployeeId
    {
    get { return m_employeeId;
    }
    set
    {
    m_employeeId = value;
    }

    }
    public string EmployeeName
    {
    get { return m_employeeName;
    }
    set
    {
    m_employeeName = value;
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    There are so many things wrong with that code that I'm not going to bother.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    OP,

    Go through these links: Encapsulation

    What are Accessors and Mutators and how to use them: Java Accessors Mutators

    You could have easily Googled them, but anyways...

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    And yes, use Code Tags next time onwards while posting your code.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    NareshN is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi,

    In encapsulation concept can anyone explain this line with example.If we 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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If all your classes fields are declared public it could cause problems for people using your code. For example, if you change from public to private, they may have code which sets variables manually, this would no longer be allowed. You encapsulate your class so they never have access to the information in the first place, this way, if you make a change, there code isn't affected.
    Last edited by sunde887; 01-19-2011 at 07:30 AM.

  7. #7
    NareshN is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi,

    Sorry i didn't get you .

    Both is same thing how we are hidindg data.can u please elaborate it.How we preventing accedental loss of data.

    This is the interview question i am facing.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I had a detailed explanation but I realized what you meant with the line you were asking about. If you create a class with private fields, you also have constructors to initialize the members, and methods that allow you to change the fields.

    Java Code:
    class person{
      private String name;
      private int pn;
      person(){
        name = "bob";
        pn = 10;
      }
      person(String name){
        this.name = name;
        pn = 10;
      }
      person(String name, int pn){
        this.name = name;
        this.pn = pn;
      }
      public String lookupname(){
        return name;
      }
      public int lookupnum(){
        return pn;
      }
    }
    
    public class loblah{
      public static void main(String[] args){
        person chris = new person("Chris", 100);
        person john = new person("John");
        person liz = new person();
        chris.lookupname();
        chris.lookupnum();
        john.lookupname();
        john.lookupnum();
        liz.lookupname();
        liz.lookupnum();
      }
    }
    in this example, for some reason you do not want people to access the data fields directly, but they need to be able to create a person object somehow. If you declare it public something you may not want happening may happen, so you declare it private and create methods to lookup the information, and constructors to initialize the person.

    if all the fields are public something may happen which doesn't cause an error but can give you results that are different with what you expect.

    same code without encapsulation:
    Java Code:
    class Person{
    	public String name;
    	public int pn;
    }
    
    public class Loblah{
    	public static void main(String[] args){
    		Person chris = new Person();
    		chris.name = "Chris";
    		chris.pn = 10;
    		Person john = new Person();
    		john.name = "John";
    		john.pn = 20;
    		Person liz = new Person();
    		liz.name = "Liz";
    		liz.pn = 30;
    		System.out.println(liz.name + " " + liz.pn);
    		System.out.println(john.name + " " + john.pn);
    		System.out.println(chris.name + " " + chris.pn);
    	}
    }
    You may have circumstances where you don't want the classes information to be able to be changed by another class.

    read that and see if it helps you at all:
    Java Tutorial 4 - Encapsulation and Classes
    Last edited by sunde887; 01-19-2011 at 07:52 AM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    As another example, slightly more complex perhaps.
    Java Code:
    public class MyDateClass {
        public Date date;
    }
    With an object of this class I can change the values in date by using date.setTime(). MyDateClass may need to do some work when a date is changed (recalculate interest, say), but now it can't do that work since it has no idea that date has changed.

    Java Code:
    public class MyBetterDateClass {
        private Date date;
    
        public Date getDate() {
            return date.clone();
        }
        public void setDate(Date date) {
            this.date = date;
            recalculateStuff();
        }
    }
    Now I'm only supplying a copy of my date class which means that no one can change my data (without cheating anyway) without me knowing, so I can now do my recalculations whenever the date changes.

Similar Threads

  1. JSTL accessing properties on an object that extends ArrayList
    By chester_perry in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2013, 06:54 PM
  2. Replies: 5
    Last Post: 10-11-2010, 02:16 AM
  3. Accessing Properties File
    By java_men in forum Java Servlet
    Replies: 0
    Last Post: 05-11-2010, 12:34 PM
  4. Accessing to file roots and their properties
    By JVega in forum Advanced Java
    Replies: 3
    Last Post: 01-25-2010, 01:25 PM
  5. Replies: 6
    Last Post: 03-31-2009, 05:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •