1

I know this is very basic but I want to know why we should use private variable in encapsulation.I am mentioning my code. so I can give you better explanation.

 Class Employee
 {
      private String eName;

      public String getEname()
      {
         return this.eName;
      }
      public void setEname(String name)
      { 
           this.eName=name;
       }

Here "eName" is private so we can restrict outside object to access directly. variable can be access using its getter setter only.

Now my question is why we should use getter setter method? can't we declare variable as a public.?

Using setter method any one can change the value so what is need of this private variable?

Another Question

we set create read/write only method if we don't create getter method then it become write only and if we don't create setter method then it become read only.

so what is the use of read only and write only ?

If we don't create setter method then how value will set to variable ?

If we don't create getter method then how value will retrieve?

Please give me the answer of above simple questions.

Thank you :)

Kapil Devmurari
  • 159
  • 3
  • 11

4 Answers4

1

The point of making variable private and

Let's say your bankBalance is a private variable and checkBalance() is a public method which is calling a public getter method getbankBalance() for bankBalance. Now by doing this I can call checkBalance() which will call getbankBalance() function and read the value of bankBalance. I will only have read only access to sensitive data via a public method only.

Now, If the variable bankBalance is public any function can access and change the value of your bank balance, I can check for packageName.ClassName.bankBalance variable and then I can change the value of that variable to 0.

In order to provide read/write only accesses we need to make variables private, protected.

Hence The need of private variables in public methods.

Hope this is good explanation.

Ajinkya Patil
  • 741
  • 1
  • 6
  • 17
  • 1
    checkBalance() is public right ? every one can access, so what is use of it. every one can call checkBalance() method using instance of class and can easily get the value of bankBalance . so how this is useful ? – Kapil Devmurari Apr 26 '16 at 13:06
  • It is making bankBalance only readable, it can be changeable only in that scope. so the operation can be done only in that scope. So making that variable safe. I can say it is some type of security for that variable. – sreedhar Nov 21 '18 at 06:57
0

If we declare the method as public, then others can change its value. Sometimes, in encapsulation, you don't want the value to be changed, but only to be seen. public members can be read, as well as be changed. Hence, you declare it private.


so what is the use of read only and write only ?

I have rarely seen write only, but read only is common, for example, in bank account softwares. You want the account balance to be read, but not to be changed!


If we don't create setter method then how value will set to variable ?

obj.eName = "Whatever";

If we don't create getter method then how value will retrieve?

whatever = obj.eName;
dryairship
  • 6,022
  • 4
  • 28
  • 54
0

in setter we can validate input data being set to property and take action accordingly, refer below :

public void setEname(String name)
      { 
          //Here we can validate input data or even throw an exception
          if(name!=null && name.length()<5){
            this.eName="Some Default Value";
            //throw new IllegalArgumentException("Your custom message goes here");
           }else{
            this.eName=name;
           }
       }

Also many api's use java reflection to set private property values, for this setter methods are required.

Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

Now my question is why we should use getter setter method? can't we declare variable as a public.?

Using setter method any one can change the value so what is need of this private variable?

The public and protected methods of your classes form part of its exported API, i.e. the contract you make with client users of your class that specifies what your class does. In other words, the accessible methods specify your class's behavior.

The fields of yout class form its state. A class's state need not, and often does not, have a dependency on its contract. In other words, the fields in your class that are hidden away form part of your class's implementation (but not its behavior or its contract).

Note that the hidden parts of an implementation are not part of the contract and are also not part of the exported API. This means that you are free to change the implementation at any time without risk of breaking your client's programs as long as the implementation continues to obey the contract of your exported API. Therefore, fields that make up the state of your objects should nearly always be made private or package-private.

All this changes when you make your fields public. In this case, clients have direct access to your class's state and so now it becomes part of the exported API. Making fields public makes your implementation part of your contract and so now you are no longer free to make any changes to that part of it. You must continue to support that public variable for as long as your class exists.

scottb
  • 9,908
  • 3
  • 40
  • 56