Results 1 to 9 of 9
- 01-19-2011, 04:42 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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;
}
}
- 01-19-2011, 04:52 AM #2
There are so many things wrong with that code that I'm not going to bother.
- 01-19-2011, 04:59 AM #3
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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-19-2011, 05:01 AM #4
And yes, use Code Tags next time onwards while posting your code.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-19-2011, 06:00 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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.
- 01-19-2011, 06:21 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 06:30 AM.
- 01-19-2011, 06:34 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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.
- 01-19-2011, 06:47 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
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.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(); } }
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:
You may have circumstances where you don't want the classes information to be able to be changed by another class.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); } }
read that and see if it helps you at all:
Java Tutorial 4 - Encapsulation and ClassesLast edited by sunde887; 01-19-2011 at 06:52 AM.
- 01-19-2011, 08:54 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
As another example, slightly more complex perhaps.
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 MyDateClass { public Date date; }
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.Java Code:public class MyBetterDateClass { private Date date; public Date getDate() { return date.clone(); } public void setDate(Date date) { this.date = date; recalculateStuff(); } }
Similar Threads
-
JSTL accessing properties on an object that extends ArrayList
By chester_perry in forum Advanced JavaReplies: 4Last Post: 01-13-2011, 10:07 AM -
accessing variable/methods of a instantiated object
By hariza in forum AWT / SwingReplies: 5Last Post: 10-11-2010, 01:16 AM -
Accessing Properties File
By java_men in forum Java ServletReplies: 0Last Post: 05-11-2010, 11:34 AM -
Accessing to file roots and their properties
By JVega in forum Advanced JavaReplies: 3Last Post: 01-25-2010, 12:25 PM -
accessing a one class's non static variable from another class
By aruna1 in forum New To JavaReplies: 6Last Post: 03-31-2009, 04:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks